How To Highlight The Grid View Item On Select?
I am creating a Grid view layout with image and text.I want multi-select of item which is working fine but i want to highlight the grid items which is selected. Here is my code: pu
Solution 1:
you can use selector to highlight item
In drawable folder create a xml file
<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@color/blue"android:state_selected="true"/><itemandroid:drawable="@color/transparent"/></selector>and set listSelector of your gridview like
android:listSelector="@drawable/list_selector"
Solution 2:
- create ImageAdapter Class
- add this parameter to your class
private int selectedPosition = -1 add this method to your ImageAdapter Class
publicvoidsetSelectedPosition(int position){ selectedPosition = position; }add to the end of your getView method of ImageAdapter Class these lines
if (position == selectedPosition) { gridView.setBackgroundColor(Color.BLACK); } else { gridView.setBackgroundColor(Color.TRANSPARENT); }- in your activity(or fragment) create object from ImageAdapter class
then just add these lines to test
GridViewgridView= (GridView) view.findViewById(R.id.gridview); gridView.setAdapter(adapterImage); gridView.setOnItemClickListener(newAdapterView.OnItemClickListener() { publicvoidonItemClick(AdapterView<?> parent, View v, int position, long id) { adapterImage.setSelectedPosition(position); adapterImage.notifyDataSetChanged(); } });
Solution 3:
if your grid view is inside a Relative layout you can set touch listener there to highlight the selected item by using a custom draw-able(here i use a nine patch image to specify the selection)
private RelativeLayout.OnTouchListenertouchListener=newRelativeLayout.OnTouchListener() {
publicbooleanonTouch(View v, MotionEvent event) {
if(touchEnabled==false)
returnfalse;
try {
if (oldView != null) {
oldView.setBackgroundResource(R.color.transparent);
}
setSelection(((ViewHolder)v.getTag()).pos);
v.setBackgroundResource(R.drawable.list_pressed);
oldView = v;
} catch (Exception e) {
}
returnfalse;
}
};
or you can use selector as well
In drawable folder create a xml file
<selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@color/blue"android:state_selected="true"/><itemandroid:drawable="@color/transparent"/></selector>and set listSelector of your gridview like
android:listSelector="@drawable/list_selector"
Solution 4:
I solved this problem with setTag on childItems
this is my sample code
yourGridView.setOnItemClickListener((parent1, view, position1, id1) -> {
if (view.getTag()!=null && view.getTag().equals("Examplename")){
view.setBackgroundColor(yourColor);
view.setTag("");
}else{
view.setTag("Examplename");
view.setBackgroundColor(Color.BLUE);
int Nums=gridListItems.getChildCount();
for (int i=0; i<=Nums-1; i++ ){
if (i!=position1){
View child= gridListItems.getChildAt(i);
child.setBackgroundColor(yourColor);
}
}
}
});
Post a Comment for "How To Highlight The Grid View Item On Select?"