abc

Share This blog with your friends, so that we can improve more & more . our aim is to easy & simple way of learning.

12/20/2021

How to add search box for list of items in android

 While using number of items like eCommerce product, library, news in such cases we use search box for finding exact item.

 for this we use below steps,

 step 1) create android studio empty project, go to res-->layout-->main.xml and add below code,

 <ListView

        android:id="@+id/lv1"

        android:layout_width="376dp"

        android:layout_height="664dp"

        android:layout_marginStart="16dp"

        android:layout_marginTop="64dp"

        android:divider="#ad5"

        android:dividerHeight="2dp"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent" />


in the same folder create menu folder(from right button click on res folder-->resource directory and select menu),

step 2) then add below code in menu.xml file.

<menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto">


    <item

        android:id="@+id/app_bar_search"

        android:icon="@android:drawable/ic_menu_search"

        android:title="Search"

        app:showAsAction="ifRoom|withText"

        app:actionViewClass="android.widget.SearchView">

    </item>

</menu>

 

step 3) finally go to mainactivity.java , and use below code.

ListView listView;

    ArrayList<String> list;

    ArrayAdapter<String> adapter;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        listView = findViewById(R.id.lv1);

        list = new ArrayList<>();

        list.add("Website");

        list.add("Software");

        list.add("Mobile app");

        list.add("php development");

        list.add("java development");

        list.add("Net development");

        list.add("mysql development");

        list.add("Html, css");

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);

        listView.setAdapter(adapter);

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.menu, menu);

        MenuItem searchViewItem = menu.findItem(R.id.app_bar_search);

        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchViewItem);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override

            public boolean onQueryTextSubmit(String query) {

                searchView.clearFocus();

                return false;

            }

            @Override

            public boolean onQueryTextChange(String newText) {

                adapter.getFilter().filter(newText);

                return false;

            }

        });

        return super.onCreateOptionsMenu(menu);

    } 

 

step 4) now it's time to test on emulator(from menu -->run-->click on run) or real android phone.

 output is like this, on top action bar click on search icon and type specific item from list.you will see how is works.




Output of Search items.

An Introduction to the Laravel Framework: What It Is and Why You Should Use It

  If you're a PHP developer looking for a modern, efficient, and powerful framework to build web applications, look no further than Lara...