Android how to change gridview highlight color?

how to change the highlight color of the grid image.

I tried this,

 public View getView(int position, View convertView, ViewGroup parent) {

    ImageView imageView;
    if (convertView == null) {  // if it not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(width, height));
     //   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]));, imageView); 

  //     id.DisplayImage(s, imageView);

    return imageView;



source to share

2 answers

I resolve this myself, you add this to your layout xml



but not






Add a file imageview_highlight_selector.xml

containing the following content to the folder drawable

and then call imageView.setBackgroundResource(R.drawable.gridview_highlight_selector);


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


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


If you only want to use color and not pull, you can add a sub-folder color

to the folder res

and add the following content as gridview_highlight_selector.xml

the folder color

and call imageView.setBackgroundResource(R.color.gridview_highlight_selector);

in your code:

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




All Articles