Created
April 2, 2018 15:30
-
-
Save alitamoor65/cf21f15e43e457c5af1ed6395658ed8b to your computer and use it in GitHub Desktop.
Search View from list view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout 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:orientation="vertical" | |
android:layout_height="match_parent" | |
tools:context="com.tutorialscafe.readallsms.MainActivity"> | |
<!-- Editext for Search --> | |
<EditText android:id="@+id/inputSearch" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:hint="Search products.." | |
android:inputType="textVisiblePassword"/> | |
<!-- List View --> | |
<ListView | |
android:id="@+id/list_view" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tutorialscafe.readallsms; | |
import android.content.DialogInterface; | |
import android.content.Intent; | |
import android.support.v7.app.AlertDialog; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ArrayAdapter; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.ListView; | |
import java.util.ArrayList; | |
import java.util.HashMap; | |
public class MainActivity extends AppCompatActivity { | |
// List view | |
private ListView lv; | |
// Listview Adapter | |
ArrayAdapter<String> adapter; | |
// Search EditText | |
EditText inputSearch; | |
// ArrayList for Listview | |
ArrayList<HashMap<String, String>> productList; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
// Listview Data | |
String products[] = {"Dell Inspiron", "HTC One X", "HTC Wildfire S", "HTC Sense", "HTC Sensation XE", | |
"iPhone 4S", "Samsung Galaxy Note 800","Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro", | |
"Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro","Samsung Galaxy S3", "MacBook Air", "Mac Mini", "MacBook Pro"}; | |
lv = (ListView) findViewById(R.id.list_view); | |
inputSearch = (EditText) findViewById(R.id.inputSearch); | |
// Adding items to listview | |
adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, products); | |
lv.setAdapter(adapter); | |
inputSearch.addTextChangedListener(new TextWatcher() { | |
@Override | |
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { | |
// When user changed the Text | |
MainActivity.this.adapter.getFilter().filter(cs); | |
} | |
@Override | |
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, | |
int arg3) { | |
// TODO Auto-generated method stub | |
} | |
@Override | |
public void afterTextChanged(Editable arg0) { | |
// TODO Auto-generated method stub | |
} | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" > | |
<!-- Single ListItem --> | |
<!-- Product Name --> | |
<TextView android:id="@+id/product_name" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:padding="10dip" | |
android:textSize="16dip" | |
android:textStyle="bold"/> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment