AutoComplete SearchView

Sometimes we want from our application, to provide suggestions to the user, when he/she types in an editable text field. We can do that via AutoCompleteTextView, that gives suggestions automatically and display them to a drop down menu, from which the user can choose a preferred item. Moreover, Android give us the opportunity to input multiple items than only one, in the editable text field by using MultiAutoCompleteTextView class. Through this example, the user is allowed to ender one and/or multiple items in an edit text, where suggestions automatically are displayed on the screen.

1. MainActivity.java

package com.tutorialsee;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import com.tutorealsee.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {
ProgressDialog mProgressDialog ;
ArrayList<String> array ;
AutoCompleteTextView att ;
Context context ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context =  this ;
att = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
array = new ArrayList<String>() ;
new getdata().execute();

}

class getdata extends AsyncTask<String, Void, String> {
InputStream inputStream = null;
String result = "";
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Loading...");
mProgressDialog.setCanceledOnTouchOutside(false);
mProgressDialog.setCancelable(false);
mProgressDialog.getWindow().setGravity(Gravity.CENTER);
mProgressDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
mProgressDialog.show();
}

@Override
protected String doInBackground(String... params) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(new HttpGet("**************Your URL **************"));
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null) {
try {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream,"UTF-8"));
String line = "";
while((line = bufferedReader.readLine()) != null)
result += line;
} catch (IOException e) {
e.printStackTrace();
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("out",""+result);
}else {
result = "Did not work!";
} } catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
protected void onPostExecute(String args) {
mProgressDialog.dismiss();
try{

for(int i = 0; i < new JSONArray(args).length() ; i++){
JSONObject jobj = new JSONArray(args).getJSONObject(i);
array.add(jobj.getString("title"));
}
Log.e("array size", " : "+array.size());

ArrayAdapter adapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,array);
att.setAdapter(adapter);

}catch(Exception e){
e.printStackTrace();
}
}
}
}

2. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fadingEdge="none" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fadingEdge="none" >

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:id="@+id/gallery_paging"
                android:layout_width="fill_parent"
                android:layout_height="270dp"
                android:background="#7F7F7F" >

                <LinearLayout
                    android:id="@+id/image_count"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="5dp"
                    android:background="#00000000"
                    android:orientation="horizontal" >
                </LinearLayout>
            </RelativeLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/gallery_paging"
                android:layout_marginTop="5dp"
                android:orientation="vertical" >

                <LinearLayout
                    android:id="@+id/houseCallLayout"
                    android:layout_width="match_parent"
                    android:layout_height="240dp"
                    android:orientation="horizontal"
                    android:weightSum="1" >

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight=".5"
                        android:background="#009688"
                        android:contentDescription="@null" />

                    <View
                        android:layout_width="6dp"
                        android:layout_height="match_parent"
                        android:background="@android:color/transparent" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight=".5"
                        android:background="#4CAF50"
                        android:orientation="vertical" >

                        <ImageView
                            android:id="@+id/nailLayout"
                            android:layout_width="wrap_content"
                            android:layout_height="120dp"
                            android:background="#FFEB3B"
                            android:contentDescription="@null" />

                        <ImageView
                            android:id="@+id/makeUpLayout"
                            android:layout_width="wrap_content"
                            android:layout_height="120dp"
                            android:layout_marginTop="5dp"
                            android:background="#03A9F4"
                            android:contentDescription="@null" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:layout_marginTop="7dp"
                    android:orientation="horizontal"
                    android:weightSum="1" >

                    <ImageView
                        android:id="@+id/BrowLayout"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight=".5"
                        android:background="#795548"
                        android:contentDescription="@null" />

                    <View
                        android:layout_width="6dp"
                        android:layout_height="match_parent"
                        android:background="@android:color/transparent" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight=".5"
                        android:background="#FF5722" >

                        <ImageView
                            android:id="@+id/facialLayout"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:background="#FF5722"
                            android:contentDescription="@null" />
                    </LinearLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="120dp"
                    android:layout_marginBottom="2dp"
                    android:layout_marginTop="7dp"
                    android:orientation="horizontal"
                    android:weightSum="1" >

                    <ImageView
                        android:id="@+id/massage"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight=".5"
                        android:background="#4CAF50"
                        android:contentDescription="@null" />

                    <View
                        android:layout_width="6dp"
                        android:layout_height="match_parent"
                        android:background="@android:color/transparent" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:layout_weight=".5"
                        android:background="#607D8B" >

                        <ImageView
                            android:id="@+id/hairLayout"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:background="#607D8B"
                            android:contentDescription="@null" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>
    </ScrollView>

    <LinearLayout
        android:id="@+id/input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="60dp"
        android:gravity="center|center_vertical" >

        <AutoCompleteTextView
            android:id="@+id/autoCompleteTextView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_weight="6.78"
            android:background="#ffffff"
            android:hint="search"
            android:padding="10dp"
            android:paddingLeft="15dp" >

            <requestFocus />
        </AutoCompleteTextView>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.93"
            android:background="#ffffff"
            android:contentDescription="@null"
            android:padding="5dp"
            android:src="@drawable/search" />
    </LinearLayout>

</RelativeLayout>

3. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tutorealsee"
    android:versionCode="1"
    android:versionName="1.0" >

    <supports-screens
        android:anyDensity="true"
        android:largeScreens="true"
        android:normalScreens="true"
        android:resizeable="true"
        android:smallScreens="true"
        android:xlargeScreens="true" />

    <uses-sdk android:minSdkVersion="8" />
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light" >
        <activity
            android:name="com.tutorialsee.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.tutorialsee.HomeActivity"
            android:label="@string/title_activity_home" >
        </activity>
    </application>

</manifest>