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.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;
}

      

+3


source to share


2 answers


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

 android:listSelector="@drawable/panel_picture_frame_background"

      

but not



imageView.setBackgroundResource(R.color.gridview_highlight_selector);

      

thank

+12


source


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="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 that you define your gridview item in an XML file and then inflate that xml from your Java code which would be neater.



EDIT:

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="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#ffff" />
    <item android:color="#ff3697de" />
</selector>

      

+3


source







All Articles