4

how can change the highlight color of a imageView inside gridview.

I've tried this,

 public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { // if it's not recycled, initialize some attributes imageView = new ImageView(mContext); imageView.setLayoutParams(new GridView.LayoutParams(width, height)); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setBackgroundResource(R.drawable.menu_beh); // imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } String s=(String)HiveApp.mgd[position].posters[2].image.url; // imageView.setImageDrawable(getPicture(items[position])); HiveApp.id.download(s, imageView); // id.DisplayImage(s, imageView); return imageView; } 

2 Answers 2

13

I resolve it my self, you shoud add this to your layout xml

 android:listSelector="@drawable/panel_picture_frame_background" 

and not this

imageView.setBackgroundResource(R.color.gridview_highlight_selector); 

thanks

Sign up to request clarification or add additional context in comments.

1 Comment

Worked for me too. Option above crashed.
3

Add an imageview_highlight_selector.xml file containing the following content to the drawable folder, and then call imageView.setBackgroundResource(R.drawable.gridview_highlight_selector);.

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/highlight_bg" /> <!-- pressed --> <item android:drawable="@drawable/normal_bg" /> <!-- default --> </selector> 

I would suggest you define your gridview item in an xml file, and then inflate that xml from inside your Java code, which would be neater.

EDIT:

If you only want to use color rather a drawable, you can add a color subfolder to the res folder, and add the following content as gridview_highlight_selector.xml to the color folder, and call imageView.setBackgroundResource(R.color.gridview_highlight_selector); in your code:

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#ffff" /> <item android:color="#ff3697de" /> </selector> 

1 Comment

Thank you for your answer, but this change the state of the background of the item, and the orange highlight don't disappear...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.