All Countries in ListView with Searching – ListView Example

Posted on .
android listview

Adding searching functionality to Android Listview is very good habit because it allows user to search faster and get the things immediately . Lets see how it works –

Download Code

STEPS – 

1. create new project in eclipse IDE  , goto File  New Android Project . Give your desire Project Name .

2). Choose your desire activity in which you want to show your Listview , i am Using activity_main.xml .

Here is my  activiy_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" >

<!-- Editext for Search -->
<EditText android:id="@+id/inputSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search products.."
android:inputType="textVisiblePassword"/>

<!-- List View -->
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>

Item_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <!-- Single ListItem -->
    
    <!-- Product Name -->
    <TextView android:id="@+id/product_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold"/>    

</LinearLayout>

3). Now open your MainActivity.java file in which you have to add array [ ] of String Type and set Listview to Adapter.

package androidarena.listviewsearch;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity {
	
	// List view
	private ListView lv;
	
	// Listview Adapter
	ArrayAdapter<String> adapter;
	
	// Search EditText
	EditText inputSearch;
	
	
	// ArrayList for Listview
	ArrayList<HashMap<String, String>> productList;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // Listview Data
        String products[] = {"Afghanistan",
        		"Albania",
        		"Algeria",
        		"Andorra",
        		"Angola",
        		"Antigua and Barbuda",
        		"Argentina",
        		"Armenia",
        		"Australia",
        		"Austria",
        		"Azerbaijan",
        		"Bahamas",
        		"Bahrain",
        		"Bangladesh",
        		"Barbados",
        		"Belarus",
        		"Belgium",
        		"Belize",
        		"Benin",
        		"Bhutan",
        		"Bolivia",
        		"Bosnia and Herzegovina",
        		"Botswana",
        		"Brazil",
        		"Brunei",
        		"Bulgaria",
        		"Burkina Faso",
        		"Burundi",
        		"Cambodia",
        		"Cameroon",
        		"Canada",
        		"Cape Verde",
        		"Central African Republic",
        		"Chad",
        		"Chile",
        		"China",
        		"Colombi",
        		"Comoros",
        		"Congo (Brazzaville)",
        		"Congo",
        		"Costa Rica",
        		"Cote d'Ivoire",
        		"Croatia",
        		"Cuba",
        		"Cyprus",
        		"Czech Republic",
        		"Denmark",
        		"Djibouti",
        		"Dominica",
        		"Dominican Republic",
        		"East Timor (Timor Timur)",
        		"Ecuador",
        		"Egypt",
        		"El Salvador",
        		"Equatorial Guinea",
        		"Eritrea",
        		"Estonia",
        		"Ethiopia",
        		"Fiji",
        		"Finland",
        		"France",
        		"Gabon",
        		"Gambia, The",
        		"Georgia",
        		"Germany",
        		"Ghana",
        		"Greece",
        		"Grenada",
        		"Guatemala",
        		"Guinea",
        		"Guinea-Bissau",
        		"Guyana",
        		"Haiti",
        		"Honduras",
        		"Hungary",
        		"Iceland",
        		"India",
        		"Indonesia",
        		"Iran",
        		"Iraq",
        		"Ireland",
        		"Israel",
        		"Italy",
        		"Jamaica",
        		"Japan",
        		"Jordan",
        		"Kazakhstan",
        		"Kenya",
        		"Kiribati",
        		"Korea, North",
        		"Korea, South",
        		"Kuwait",
        		"Kyrgyzstan",
        		"Laos",
        		"Latvia",
        		"Lebanon",
        		"Lesotho",
        		"Liberia",
        		"Libya",
        		"Liechtenstein",
        		"Lithuania",
        		"Luxembourg",
        		"Macedonia",
        		"Madagascar",
        		"Malawi",
        		"Malaysia",
        		"Maldives",
        		"Mali",
        		"Malta",
        		"Marshall Islands",
        		"Mauritania",
        		"Mauritius",
        		"Mexico",
        		"Micronesia",
        		"Moldova",
        		"Monaco",
        		"Mongolia",
        		"Morocco",
        		"Mozambique",
        		"Myanmar",
        		"Namibia",
        		"Nauru",
        		"Nepal",
        		"Netherlands",
        		"New Zealand",
        		"Nicaragua",
        		"Niger",
        		"Nigeria",
        		"Norway",
        		"Oman",
        		"Pakistan",
        		"Palau",
        		"Panama",
        		"Papua New Guinea",
        		"Paraguay",
        		"Peru",
        		"Philippines",
        		"Poland",
        		"Portugal",
        		"Qatar",
        		"Romania",
        		"Russia",
        		"Rwanda",
        		"Saint Kitts and Nevis",
        		"Saint Lucia",
        		"Saint Vincent",
        		"Samoa",
        		"San Marino",
        		"Sao Tome and Principe",
        		"Saudi Arabia",
        		"Senegal",
        		"Serbia and Montenegro",
        		"Seychelles",
        		"Sierra Leone",
        		"Singapore",
        		"Slovakia",
        		"Slovenia",
        		"Solomon Islands",
        		"Somalia",
        		"South Africa",
        		"Spain",
        		"Sri Lanka",
        		"Sudan",
        		"Suriname",
        		"Swaziland",
        		"Sweden",
        		"Switzerland",
        		"Syria",
        		"Taiwan",
        		"Tajikistan",
        		"Tanzania",
        		"Thailand",
        		"Togo",
        		"Tonga",
        		"Trinidad and Tobago",
        		"Tunisia",
        		"Turkey",
        		"Turkmenistan",
        		"Tuvalu",
        		"Uganda",
        		"Ukraine",
        		"United Arab Emirates",
        		"United Kingdom",
        		"United States",
        		"Uruguay",
        		"Uzbekistan",
        		"Vanuatu",
        		"Vatican City",
        		"Venezuela",
        		"Vietnam",
        		"Yemen",
        		"Zambia",
        		"Zimbabwe"};
        
        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);
        
        // Adding items to listview
        adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products);
        lv.setAdapter(adapter);
        
        /**
         * Enabling Search Filter
         * */
        inputSearch.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
				// When user changed the Text
				MainActivity.this.adapter.getFilter().filter(cs);	
			}
			
			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub							
			}
		});
    }
    
}

Output –

output

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="androidarena.listviewsearch"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="androidarena.listviewsearch.MainActivity"
            android:label="@string/title_activity_main"
            android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Final Output –

final ouput

For more Query do comment and Post on this Tutorial . enjoy and happy Coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *