Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>we can use this code, </p> <pre><code>public class MyGridView extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.animalsgridview); GridView gridview = (GridView) findViewById(R.id.gridviewid); gridview.setAdapter(new ImageAdapter(this)); gridview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) { int imageId = ((ImageAdapter)parent.getAdapter()).mygetItemId(position); Intent fullScreenIntent = new Intent(v.getContext(),Full.class); fullScreenIntent.putExtra(Full.class.getName(),imageId); AnimalsGridView.this.startActivity(fullScreenIntent); } }); } private class ImageAdapter extends BaseAdapter { private Context mContext; public ImageAdapter(Context c) { mContext = c; } public int getCount() { return imagearray.length; } public Object getItem(int position) { return null; } public long getItemId(int position) { return imagearray[position]; } // create a new ImageView for each item referenced by the Adapter 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(150, 150)); imageView.setScaleType(ImageView.ScaleType.FIT_XY); imageView.setPadding(1, 1, 1, 1); } else { imageView = (ImageView) convertView; } imageView.setImageResource(imagearray[position]); return imageView; } // references to our images private Integer[] imagearray = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 }; public int mygetItemId(int position) { return imagearray[position]; } } } </code></pre> <p>this is the xml code </p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_weight="1" android:layout_height="fill_parent"&gt; &lt;GridView android:id="@+id/gridviewid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numColumns="auto_fit" android:columnWidth="60dp" android:stretchMode="columnWidth" android:padding="5dp" android:verticalSpacing="5dp" android:horizontalSpacing="20dp" android:gravity="center_horizontal" android:background="#ffffff"/&gt; </code></pre> <p></p>
 

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