Search View

It is a default control provided by the Android for developers. It is a part of widget which provides users to send requested query to the server side and get the required result. This control receives data or query in audio and text form both. Through searchView, we can create a suggestion for the user on search based on previous queries and enable voice searching. There are various attributes and methods which are helping to develop searchView Interface for the user.

ATTRIBUTES

android:iconifiedByDefault: It is used to set default state of search

android:QueryHint: We can set the query hint on the basis of users requirement

android:inputType: InputType defines the type of text entered in text field

android:maxWidth: To set the maximum number of characters allowed for searching

android:imeOptions: It is used to set IME (Input Method Editor) option in Text field

METHODS

setonQueryClickListener(): It is clickable event handling when user pressed the button

setonSearchTextListener(): This method sets a listener for users action with searchView

setonSuggestionListener(): This listener method invoke when the suggestion text is focused or click

setonQueryTextFocusListener(): It is used to set listener for requested query when text is focused

setQueryHint(): To provide hints for user query when text is typed in text field

setQuery(): To set the query which is based on user data searched previously

getQueryHint(): This method is used to get the query hint set in searchView Box

getImeOptions(): It returns the IME options which is set in the query text field

setSuggestionAdapater(): If we wish to create custom adapter for search

setSubmitButtonEnabled(): Whether the submit button is Enabled or not

Main_Activity.xml

<? Xml version="1.0" encoding="utf-8"?>
<LinearLayout
   android: orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.mapsample.msclient009.notification.MainActivity” >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android: orientation="vertical">
        <Search View
            android: id="@+id/searchBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android: clickable="true" />
        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/list" />
    </LinearLayout>
</LinearLayout>

Main_Activity.java
Package com.mapsample.msclient009.searchView;
import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import com.journaldev.searchview.databinding.ActivityMainBinding;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    ActivityMainBinding bind;
    ListAdapter adapter;
    List arrayList= new ArrayList();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bind = DataBindingUtil.setContentView(this, R.layout.activity_main);
        arrayList.add("Java");
        arrayList.add("scala");
        arrayList.add("Fortan");
        arrayList.add("C");
        arrayList.add("C++");
        arrayList.add("Andriod");
        arrayList.add("BAsic");
        arrayList.add("C#");
        arrayList.add("PHP");
        arrayList.add(“Python");
        arrayList.add("SQL");
        arrayList.add("SQLIte");
        adapter= new ListAdapter(arrayList);
        bind.listView.setAdapter(adapter);
        bind.search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String ntext) {
                adapter.getFilter().filter(ntext);
                return false;
            }
        });
    }
}

List.java
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import com.journaldev.searchview.databinding.RowItemBinding;
import java.util.ArrayList;
import java.util.List;
public class ListAdapter extends BaseAdapter implements Filterable {
    List list;
    List data;
    ValueFilter filterValuer;
    private LayoutInflater inflater;
    public ListAdapter(List ctype) {
        list =cype;
        filterValue = cancel_type;
    }
    @Override
    public int getCount() {
        return list.size();
    }
    @Override
    public String getItem(int pos) {
        return list.get(pos;
    }
    @Override
    public long getItemId(int pos) {
        return pos;
    }
    @Override
    public View getView(int pos, View convert, final ViewGroup parent) {
        if (inflater == null) {
            inflater = (LayoutInflater) parent.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        RowItemBinding itemBinder = DataBindingUtil.inflate(inflater, R.layout.item, parent, false);
        ItemBinder.stringName.setText(list.get(pos));
        return ItemBinder.getRoot();
    }
    @Override
    public Filter getFilter() {
        if (valueFilter == null) {
            valueFilter = new ValueFilter();
        }
        return valueFilter;
    }
    private class ValueFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults results = new FilterResults();
            if (constraint != null && constraint.length() > 0) {
                List flist = new ArrayList();
                for (int i = 0; i < list.size(); i++) {
                    if ((filterValue.get(i).toUpperCase()).contains(constraint.toString().toUpperCase())) {
                        filterList.add(filterValue.get(i));
                    }
                }
                results.count = flist.size();
                results.values = flist;
            } else {
                results.count = filterValue.size();
                results.values = filterValuet;
            }
            return results;
        }
        @Override
        protected void publishResults(CharSequence constraint,
                                      FilterResults results) {
            list = (List) results.values;
            notifyDataSetChanged();
        }
    }
}

List.xml
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
     android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.mapsample.msclient009.notification.MainActivity >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/stringName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:padding="@dimen/activity_horizontal_margin"
            android:textAllCaps="false"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
</Linearlayout>


  Modified On Mar-27-2018 04:00:27 AM

Leave Comment