Android Listview With SearchView and OnItemClickListener
In this tutorial you will learn the following.
- Creating android listview with image,text and description.
- Adding SearchView on ActionBar/ ToolBar.
- Making listview filterable.
- Searching listview using searchview.
- Passing listview item data from one activity to another
- Displaying clicked listview item in another activity.
Steps of creating android Listview with Searchview and onItemClickListener.
- Create new project => New => Project => Empty Activity => Name => Finish.
- Create Modal.java that will contain getter and setter for our items.
- Create row_items.xml will display items.
- Copy Images in your drawable folder.
- Download Images using the button below
- Create Menu Resource File and add Search View item.
- Create New Activity to display Clicked Items.
- Code
Android Manifest file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:dist="http://schemas.android.com/apk/distribution" package="net.larntech.searchview"> <dist:module dist:instant="true" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".ItemsPreviewActivity"></activity> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
MainActivity.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?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=".MainActivity"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> |
activity_items_preview.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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="match_parent" tools:context=".ItemsPreviewActivity"> <ImageView android:id="@+id/imageView" android:layout_width="80dp" android:layout_centerInParent="true" android:layout_height="80dp"/> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:textSize="18sp" android:layout_marginTop="10dp" android:layout_below="@id/imageView" android:layout_centerHorizontal="true" android:layout_height="wrap_content"/> </RelativeLayout> |
row_items.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_margin="8dp" android:layout_height="wrap_content"> <ImageView android:id="@+id/images" android:layout_width="50dp" android:layout_centerVertical="true" android:src="@drawable/apple" android:layout_height="50dp"/> <TextView android:id="@+id/name" android:layout_width="match_parent" android:text="Names" android:layout_marginStart="60dp" android:layout_height="wrap_content" android:layout_marginLeft="60dp" /> <TextView android:id="@+id/email" android:layout_width="match_parent" android:text="Emails" android:layout_marginStart="60dp" android:paddingTop="2dp" android:layout_below="@id/name" android:layout_height="wrap_content" android:layout_marginLeft="60dp" /> </RelativeLayout> </LinearLayout> |
menu.xml
1 2 3 4 5 6 7 8 9 10 11 |
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:myapp="http://schemas.android.com/tools"> <item android:id="@+id/searchView" android:title="Search" android:icon="@android:drawable/ic_menu_search" app:showAsAction="always" app:actionViewClass="android.widget.SearchView" /> </menu> |
ItemsModel.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.io.Serializable; public class ItemsModel implements Serializable { private String name; private String email; private int images; public ItemsModel(String name, String email,int images) { this.name = name; this.email = email; this.images = images; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getImages() { return images; } public void setImages(int images) { this.images = images; } } |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.Filter; import android.widget.Filterable; import android.widget.ImageView; import android.widget.ListView; import android.widget.SearchView; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { String names[] = {"Apple","Banana","Kiwi","Oranges","Watermelon"}; String emails[] = {"This is apple","This is banana","This is kiwi","This is oranges","This is watermelon"}; int images[] = {R.drawable.apple,R.drawable.banana,R.drawable.kiwi,R.drawable.oranges,R.drawable.watermelon}; List<itemsmodel> itemsModelList = new ArrayList<>(); ListView listView; CustomAdapter customAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.listview); for(int i = 0;i < names.length;i++){ ItemsModel itemsModel = new ItemsModel(names[i],emails[i],images[i]); itemsModelList.add(itemsModel); } customAdapter = new CustomAdapter(itemsModelList,this); listView.setAdapter(customAdapter); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.search_menu,menu) MenuItem menuItem = menu.findItem(R.id.searchView); SearchView searchView = (SearchView) menuItem.getActionView(); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override public boolean onQueryTextChange(String newText) { Log.e("Main"," data search"+newText); customAdapter.getFilter().filter(newText); return true; } }); return true; } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { int id = item.getItemId() if(id == R.id.searchView){ return true; } return super.onOptionsItemSelected(item); } public class CustomAdapter extends BaseAdapter implements Filterable { private List<itemsmodel> itemsModelsl; private List<itemsmodel> itemsModelListFiltered; private Context context; public CustomAdapter(List<itemsmodel> itemsModelsl, Context context) { this.itemsModelsl = itemsModelsl; this.itemsModelListFiltered = itemsModelsl; this.context = context; } @Override public int getCount() { return itemsModelListFiltered.size(); } @Override public Object getItem(int position) { return itemsModelListFiltered.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view = getLayoutInflater().inflate(R.layout.row_items,null); TextView names = view.findViewById(R.id.name); TextView emails = view.findViewById(R.id.email); ImageView imageView = view.findViewById(R.id.images); names.setText(itemsModelListFiltered.get(position).getName()); emails.setText(itemsModelListFiltered.get(position).getEmail()); imageView.setImageResource(images[position]); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("main activity","item clicked"); startActivity(new Intent(MainActivity.this,ItemsPreviewActivity.class).putExtra("items",itemsModelListFiltered.get(position))); } }); return view; } @Override public Filter getFilter() { Filter filter = new Filter() { @Override protected FilterResults performFiltering(CharSequence constraint) { FilterResults filterResults = new FilterResults(); if(constraint == null || constraint.length() == 0){ filterResults.count = itemsModelsl.size(); filterResults.values = itemsModelsl; }else{ List<itemsmodel> resultsModel = new ArrayList<>(); String searchStr = constraint.toString().toLowerCase(); for(ItemsModel itemsModel:itemsModelsl){ if(itemsModel.getName().contains(searchStr) || itemsModel.getEmail().contains(searchStr)){ resultsModel.add(itemsModel); filterResults.count = resultsModel.size(); filterResults.values = resultsModel; } } } return filterResults; } @Override protected void publishResults(CharSequence constraint, FilterResults results) { itemsModelListFiltered = (List<itemsmodel>) results.values; notifyDataSetChanged(); } }; return filter; } } } |
1 |
ItemsPreviewActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.ImageView; import android.widget.TextView; public class ItemsPreviewActivity extends AppCompatActivity { ImageView imageView; TextView textView; ItemsModel itemsModel; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_items_preview); imageView = findViewById(R.id.imageView); textView = findViewById(R.id.textView); Intent intent = getIntent(); if(intent.getExtras() != null){ itemsModel = (ItemsModel) intent.getSerializableExtra("items"); imageView.setImageResource(itemsModel.getImages()); textView.setText(itemsModel.getName()); } } } |
Run the project.
Download project source code. https://github.com/larntech/listview-with-search.git
Post Views:
2,513
Related
Related Posts
-
GridView With CardView , Search and ClickListener
2 Comments | Jul 30, 2020 -
Android Retrofit – Login Register using Mysql Server
No Comments | Sep 1, 2018 -
WorkManager – Scheduling Tasks Using WorkManager
No Comments | Sep 24, 2019 -
Recyclerview CardView Tutorial | RecylerView Tutorial
3 Comments | Aug 2, 2020