Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to search from a list in android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 228
    Comment on it

    We have a search edit text and a list in layout , activity_main.xml

     

    <RelativeLayout 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"
        tools:context=".MainActivity">
        <EditText
            android:id="@+id/etSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:hint="@string/search" />
        <ListView
            android:id="@+id/lvMonth"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/etSearch" />
    </RelativeLayout>

     

    In our corresponding java class, MainActivity.java

     

    public class MainActivity extends AppCompatActivity {
        private EditText search;
        private ArrayAdapter<String> month;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ListView listView = (ListView) findViewById(R.id.lvMonth);
            search = (EditText) findViewById(R.id.etSearch);
            String[] value = new String[]{"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
            month = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, value);
            listView.setAdapter(month);
            search.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    MainActivity.this.month.getFilter().filter(s);
                }
                @Override
                public void afterTextChanged(Editable s) {
                }
            });
        }
    }

     

     

    Here I have used addTextChangeListener to edit text and on TextChanged method,i have use filter method to filter the result and show result accordingly.

 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: