Set Background in GridView by item position
11,760
Solution 1
Use this in your getView()
and see if your Background changes:
gridView.getChildAt(position).setBackgroundColor(Color.RED);
Don't forget to use @Override
on getView()
:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
}
Solution 2
Try this
@Override
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(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
if(position==1)
imageview.setBackgroundColor(Color.RED);
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
Solution 3
You can use setBackground for Item Adapter.
Try this. item_image.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/bg_item_selection">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp" />
</LinearLayout>
bg_item_selection.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/white_color" android:state_pressed="false"/>
<item android:drawable="@color/black"/>
In Adapter.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ItemUserEntity itemUserEntity = new ItemUserEntity();
itemUserEntity = arrayList.get(position);
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_image, parent,
false);
}
return convertView;
}
Author by
Dennis
Updated on July 15, 2022Comments
-
Dennis almost 2 years
I want to change the background color for specific item in GridView (by position).
gridview.getChildAt(1).setBackgroundResource(android.R.drawable.btn_default);
doesn't work.
If I use it in OnClickListener it works:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { view.setBackgroundResource(android.R.drawable.btn_default); }
but I want to change it without a click.
EDIT
ImageAdapter:
public class ImageAdapter extends BaseAdapter { private final String TAG = getClass().toString(); private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return 0; } 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(85, 85)); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setPadding(8, 8, 8, 8); } else { imageView = (ImageView) convertView; } parent.getChildAt(1).setBackgroundColor(Color.RED); imageView.setImageResource(mThumbIds[position]); return imageView; }
Activity:
final GridView gridview = (GridView) view.findViewById(R.id.gridView); gridview.setAdapter(new ImageAdapter(MainActivity.this)); gridview.setSelection(0); gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() { View previous = null; @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if(previous == view) { view.setBackgroundResource(android.R.drawable.btn_default); } else { view.setBackgroundResource(android.R.drawable.btn_default); if(previous != null) previous.setBackgroundResource(0); previous = view; } } });
XML:
<GridView android:id="@+id/gridView" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:numColumns="4" > </GridView>
-
Dennis about 11 yearsThe
parent
in getView() is the gridView? -
Dennis about 11 years
GridView gridview = (GridView) parent.findViewById(R.id.gridView); gridview.getChildAt(0).setBackgroundResource(android.R.drawable.btn_default);
andparent..getChildAt(0).setBackgroundResource(android.R.drawable.btn_default);
doesn't work. -
sebster about 11 yearsin your case yes, it's the GridView
-
Dennis about 11 years
parent.getChildAt(1).setBackgroundResource(android.R.drawable.btn_default);
doesn't work =\ -
sebster about 11 yearstry it with .setBackgroundColor(Color.RED); and see if it changes color
-
sebster about 11 yearsedit your question and post your entire code for the class where you use the GridView
-
sebster about 11 yearsare you using a selector in your drawable resources?.. also post the related XML files
-
sebster about 11 yearsAlso try not to use android.R.drawable.btn_default. Instead create a selector inside your drawable resource folder and use that one to set states.
-
Dennis about 11 yearsI posted the adapter, activity and the xml file. Maybe it's important - my grid view is in a custom dialog. Thank you for your help!