Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid GridView Selected Item Background color change while pressing
    text
    copied!<p>I have the imageview using gridview in my android application.so while pressing the image in gridview,the background color want to change.if i return ,it will come to normal background color.How to do this?can anyone help me to solve this problem?</p> <p>Here my code</p> <pre><code>public class CustomGridViewExample extends Activity { private Integer[] mThumbIds = { R.drawable.android_2, R.drawable.android_2, R.drawable.android_2, R.drawable.android_2, R.drawable.android_2, R.drawable.android_2, R.drawable.android_2, R.drawable.android_2, R.drawable.android_2, R.drawable.android_2, }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new MyAdapter(this)); gridview.setNumColumns(4); } public class MyAdapter extends BaseAdapter { private Context mContext; public MyAdapter(Context c) { mContext = c; } @Override public int getCount() { return mThumbIds.length; } @Override public Object getItem(int arg0) { return mThumbIds[arg0]; } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View grid; if(convertView==null){ grid = new View(mContext); LayoutInflater inflater=getLayoutInflater(); grid=inflater.inflate(R.layout.mygrid_layout, parent, false); }else{ grid = (View)convertView; } ImageView imageView = (ImageView)grid.findViewById(R.id.image); imageView.setImageResource(mThumbIds[position]); return grid; } } </code></pre> <p>And related XML file is below : </p> <pre><code>&lt;GridView android:id="@+id/gridview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:verticalSpacing="10dp" android:horizontalSpacing="10dp" android:stretchMode="columnWidth" android:gravity="center" android:scrollbars="none" /&gt; mygrid_layout.xml &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/customshape_header" android:orientation="vertical"&gt; &lt;ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="wrap_content"/&gt; &lt;/RelativeLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload