Vervatovskis
Vervatovskis

Reputation: 2367

Android how to change gridview highlight color?

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

Upvotes: 4

Views: 14547

Answers (2)

Vervatovskis
Vervatovskis

Reputation: 2367

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

Upvotes: 13

neevek
neevek

Reputation: 12138

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>

Upvotes: 3

Related Questions