Wednesday, 26 March 2014

SearchView Functionality in Android Apps

When user want to get data from a list data quickly SearchView widget in android assist the user by filtering according to alphabet character or position of data. This blog describe about simple static search view functionality.



main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search View"
        android:textColor="#336633"
        android:textSize="20sp"
        android:textStyle="bold"
        android:layout_marginTop="20dp"
        android:layout_gravity="center"/>



    <SearchView
        android:id="@+id/searchView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp" >
    </SearchView>

</LinearLayout>

SearchViewActivity.java

package com.rakesh.tiwari.searchview;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Toast;

public class SearchViewActivity extends Activity {
    SearchView searchView;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        searchView = (SearchView) findViewById(R.id.searchView1);
        searchView.setQueryHint("Search View");

        // Set query search text focus change listener...........................................

        searchView
                .setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getBaseContext(),
                                String.valueOf(hasFocus), Toast.LENGTH_LONG)
                                .show();
                    }
                });

        // Set on query text  listener.............................................................

        searchView.setOnQueryTextListener(new OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                // TODO Auto-generated method stub
               
                Toast.makeText(getBaseContext(), query,
                        Toast.LENGTH_LONG).show();
               
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // TODO Auto-generated method stub
               
                Toast.makeText(getBaseContext(), newText,
                        Toast.LENGTH_LONG).show();
               
                return false;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search_view, menu);
        return true;
    }

}

AndroidManifest.xml



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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.rakesh.tiwari.searchview.SearchViewActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>









SearchView Functionality
SearchView Functionality

SearchView Widget
SearchView Widget

Simple SearchView Android Widget
Simple SearchView Android Widget