Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Search in a List using Search Button in ToolBar?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.12k
    Comment on it

    1)In your XML,make a ListView:

    <ListView
        android:id="@+id/mylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
    


    2)Go to menu.xml and add a Search Button:

    <item android:id="@+id/action_search"
        android:title="search"
        android:icon="@drawable/ic_launcher"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView" />
    


    3)Go to Corresponding Java File and code will be as Follows:

    public class MainActivity extends AppCompatActivity {
        ArrayAdapter<String> myAdapter;
        ListView mylist;
        String[] dataArray = new String[]{"India", "Bangladesh", "Pakistan", "Srilanka", "Nepal", "Japan"};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mylist = (ListView) findViewById(R.id.mylist);
            myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataArray);
            mylist.setAdapter(myAdapter);
            mylist.setTextFilterEnabled(true);
            mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                        long arg3) {
                    System.out.println(arg2 + " --postion");
                }
            });
        }
    

    It will have ArrayAdapter of type String,List in your XML defined and Array of String which will have values we want to display in our List.


    4)Now in your onCreateOptionsMenu(Menu menu),add SearchManager and then get it's Id by using SearchView:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
        try {
            SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextChange(String newText) {
                    // this is your adapter that will be filtered
                    myAdapter.getFilter().filter(newText);
                    System.out.println("on text chnge text: " + newText);
                    return true;
                }
                @Override
                public boolean onQueryTextSubmit(String query) {
                    // this is your adapter that will be filtered
                    myAdapter.getFilter().filter(query);
                    System.out.println("on query submit: " + query);
                    return true;
                }
            };
            searchView.setOnQueryTextListener(textChangeListener);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return true;
    }
    

    Here by adding OnQueryTextChangeListener,it will search for the text you enter Search Bar and searches for the Data in List.

    That's it.Your code is Ready and will work smoothly:)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: