Tuesday, 23 October 2012

Android Search Functionality with ListView same as Google search functionality


Hello Friends,

Hello Friends , have you searching for Google like search functionality in your Android
application ??

custom listview search
Search bar in android
custom listview search in android
search icon inside edit text



1. Create a new project in Eclipse File New ⇒ Android ⇒ Application Project and fill the required details.
2. Create required files needed to generate a listview. I am using my default activity_main.xml as listview and created a new xml file for single listitem named listview.xml
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/EditText01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@android:drawable/ic_menu_search"
        android:hint="Search" >
    </EditText>

    <ListView
        android:id="@+id/ListView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>



listview.xml




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_dark"
    android:gravity="left|center"
    android:paddingBottom="5px"
    android:paddingLeft="5px"
    android:paddingTop="5px" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10px"
        android:textColor="#0099CC"
        android:textSize="20px"
        android:textStyle="bold" >
    </TextView>

</LinearLayout>


MainActivity.java


package com.mukesh.customsearch;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

        EditText edittext;
        ListView listview;

        String[] text = { "One", "HTC One X", "Samsung Galaxy S3", "Samsung 
         Galaxy Note 800", "HTC Sense", "HTC Sensation XE", "HTC Wildfire S",
                        "HTC Wildfire", "Wildfire S", "HTC" };

        int textlength = 0;

        ArrayList<String> text_sort = new ArrayList<String>();
        ArrayList<Integer> image_sort = new ArrayList<Integer>();

        public void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                edittext = (EditText) findViewById(R.id.EditText01);
                listview = (ListView) findViewById(R.id.ListView01);
                listview.setAdapter(new CustomAdapter(text));

                edittext.addTextChangedListener(new TextWatcher() {

                        public void afterTextChanged(Editable s) {

                        }

                        public void beforeTextChanged(CharSequence s, int start,                                 int count,
                                        int after) {

                        }

                        public void onTextChanged(CharSequence s, int start, int                                       before,
                                        int count) {

                                textlength = edittext.getText().length();
                                text_sort.clear();
                                image_sort.clear();

                                for (int i = 0; i < text.length; i++) {
                                        String name = text[i];
                                        String searchtext = edittext.getText()
                                                        .toString();
                                        if (textlength <= text[i].length()) {
                                                if (name.toLowerCase().indexOf
                                             (searchtext.toLowerCase())!= -1) {
                                                        text_sort.add(text[i]);
                                                }
                                        }
                                }

                        listview.setAdapter(new CustomAdapter(text_sort));

                        }
                });
        }

        class CustomAdapter extends BaseAdapter {

                String[] data_text;

                CustomAdapter() {

                }

                CustomAdapter(String[] text) {
                        data_text = text;
                }

                CustomAdapter(ArrayList<String> text) {

                        data_text = new String[text.size()];

                        for (int i = 0; i < text.size(); i++) {
                                data_text[i] = text.get(i);
                        }

                }

                public int getCount() {
                        return data_text.length;
                }

                public String getItem(int position) {
                        return null;
                }

                public long getItemId(int position) {
                        return position;
                }

                public View getView(int position, View convertView,
                                         ViewGroup parent) {

                        LayoutInflater inflater = getLayoutInflater();
                        View row;

                        row = inflater.inflate(R.layout.listview, parent, false);

                        TextView textview = (TextView) row.findViewById(R.id.Text                                               View01);

                        textview.setText(data_text[position]);

                        return (row);

                }
        }

}




Enjoy Coding :)





Search in Custom Listview in Android | Android Custom Search | Custom Listview


Hello Friends ,

Today , I am Sharing my code my code with the help of which we can easily implements the
search functionality on listview or on a custom list view.

android search
android search bar

android search
Search  icon inside edit text 




1. Create a new project in Eclipse File New ⇒ Android ⇒ Application Project and fill the required details.
2. Create required files needed to generate a list view. I am using my default activity_main.xml as list view and created a new xml file for single list item named listview.xml
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

    <EditText

        android:id="@+id/EditText01"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"
         
        android:drawableLeft="@android:drawable/ic_menu_search"      

        android:hint="Search" >

    </EditText>

    <ListView

        android:id="@+id/ListView01"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" >

    </ListView>

</LinearLayout>



listview.xml




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/background_dark"
    android:gravity="left|center"
    android:paddingBottom="5px"
    android:paddingLeft="5px"
    android:paddingTop="5px" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10px"
        android:textColor="#0099CC"
        android:textSize="20px"
        android:textStyle="bold" >
    </TextView>

</LinearLayout>


MainActivity.java


package com.mukesh.customsearch;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

 EditText edittext;
 ListView listview;

 String[] text = { "One", "HTC One X", "Samsung Galaxy S3", "Samsung 
         Galaxy Note 800", "HTC Sense", "HTC Sensation XE", "HTC Wildfire S",
   "HTC Wildfire", "Wildfire S", "HTC" };

 int textlength = 0;

 ArrayList<String> text_sort = new ArrayList<String>();
 ArrayList<Integer> image_sort = new ArrayList<Integer>();

 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  edittext = (EditText) findViewById(R.id.EditText01);
  listview = (ListView) findViewById(R.id.ListView01);
  listview.setAdapter(new CustomAdapter(text));

  edittext.addTextChangedListener(new TextWatcher() {

   public void afterTextChanged(Editable s) {

   }

   public void beforeTextChanged(CharSequence s, int start,                                 int count,
     int after) {

   }

   public void onTextChanged(CharSequence s, int start, int                                       before,
     int count) {

    textlength = edittext.getText().length();
    text_sort.clear();
    image_sort.clear();

    for (int i = 0; i < text.length; i++) {
     if (textlength <= text[i].length()) {
      if (edittext
        .getText()
        .toString()
        .equalsIgnoreCase                                                                    (
          (                                                                  String) text[i]                                     .subSequence(0,textlength))) {
       text_sort.add(text[i]);
      }
     }
    }

    listview.setAdapter(new CustomAdapter(text_sort));

   }
  });
 }

 class CustomAdapter extends BaseAdapter {

  String[] data_text;

  CustomAdapter() {

  }

  CustomAdapter(String[] text) {
   data_text = text;
  }

  CustomAdapter(ArrayList<String> text) {

   data_text = new String[text.size()];

   for (int i = 0; i < text.size(); i++) {
    data_text[i] = text.get(i);
   }

  }

  public int getCount() {
   return data_text.length;
  }

  public String getItem(int position) {
   return null;
  }

  public long getItemId(int position) {
   return position;
  }

  public View getView(int position, View convertView,
                                         ViewGroup parent) {

   LayoutInflater inflater = getLayoutInflater();
   View row;

   row = inflater.inflate(R.layout.listview, parent, false);

   TextView textview = (TextView) row.findViewById(R.id.Text                                               View01);

   textview.setText(data_text[position]);

   return (row);

  }
 }

}




Enjoy Coding :)


Blog Related to this :
1. Horizontal ListView


Monday, 8 October 2012

Multipart image upload in android

Hello Friends,

I'm trying to upload image to my server in Android..???

After spending a lots of time , I came across Multipart image Upload.Using this 
we can easily upload image as well as file on our server.
Note: The method upload the image using http multipart form data.
           In my condition , My web web service method(upload_image) requires
           three parameter .
            1. A text message
            2. Image or Template Id
            3. Icon or Image in byte array format with filename.
   Also , I have to pass user token which i am saving into share preference,
   when user successfully logged in. Also the API Version which is nothing
   but a string(e.g 1.1) which will be saved in my Config Class.
   Please , Change the above parameter and post url as per your requirement. 
           
Note: Here , Instead of using android default HTTP client , I am using the 
          Apache Http client. Download it from  Here


     And placed it inside your project lib folder.


      /**
  * Method uploads the image using HTTP Multipart form data.
  * 
  * @param imageData
  * @param filename
         * @param icon   
  * @return
  * @throws Exception
  */



public static boolean uploadImage(final byte[] imageData, String filename ,String message) throws Exception{

        String responseString = null;       

        PostMethod method;

        String auth_token = Preference.getAuthToken(mContext);


        method = new PostMethod("http://10.0.2.20/"+ "upload_image/" +Config.getApiVersion()
               + "/"     +auth_token); 

                org.apache.commons.httpclient.HttpClient client = new              
                                            org.apache.commons.httpclient.HttpClient();

                client.getHttpConnectionManager().getParams().setConnectionTimeout(

                                100000);

                FilePart photo = new FilePart("icon", 
                                                      new ByteArrayPartSource( filename, imageData));

                photo.setContentType("image/png");
                photo.setCharSet(null);
                String s    =   new String(imageData);
               Part[] parts = {
                                new StringPart("message_text", message),
                                new StringPart("template_id","1"),
                                photo
                                };

                method.setRequestEntity(new 
                                              MultipartRequestEntity(parts, method.getParams()));
                client.executeMethod(method);
                responseString = method.getResponseBodyAsString();
                method.releaseConnection();

                Log.e("httpPost", "Response status: " + responseString);

        if (responseString.equals("SUCCESS")) {
                return true;
        } else {
                return false;
        }
    } 


Hope This Will Helps Some one.
Cheers :)
Enjoy Coding... :)

 

Copyright @ 2013 Android Developer Blog.