Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this :</p> <pre><code> public class GridActivity extends Activity implements AdapterView.OnItemClickListener { /** Called when the activity is first created. */ Integer[] imageIDs = { R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, }; String[] titles = { "First", "Second", "Third", "Fourth", "Fifth", "First", "Second", "Third", "Fourth", "Fifth", "First", "Second", "Third", "Fourth", "Fifth", "First", "Second", "Third", "Fourth", "Fifth", "First", "Second", "Third", "Fourth", "Fifth", "First", "Second", "Third", "Fourth", "Fifth", }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); GridView gridView = (GridView) findViewById(R.id.grid); gridView.setAdapter(new ImageAdapter(this)); } public class ImageAdapter extends BaseAdapter { private Context context; public ImageAdapter(Context c) { context = c; } //---returns the number of images--- public int getCount() { return imageIDs.length; } //---returns the ID of an item--- public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } //---returns an ImageView view--- public View getView(int position, View convertView, ViewGroup parent) { ImageView icon; icon = new ImageView(context); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View row=inflater.inflate(R.layout.row, parent, false); TextView label=(TextView)row.findViewById(R.id.image_name); label.setText(titles[position]); icon=(ImageView)row.findViewById(R.id.album_image); icon.setImageResource(imageIDs[position]); return row; } } @Override public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } } </code></pre> <p><strong>Main.XMl</strong></p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;GridView android:id="@+id/grid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_below="@+id/lay" android:columnWidth="100px" android:horizontalSpacing="5dip" android:numColumns="auto_fit" android:scrollbars="horizontal" android:stretchMode="columnWidth" android:verticalSpacing="35px" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Row.xml</p> <pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;ImageView android:id = "@+id/album_image" android:adjustViewBounds="true" android:layout_width = "fill_parent" android:layout_height="wrap_content" /&gt; &lt;TextView android:id = "@+id/image_name" android:layout_width = "fill_parent" android:layout_height="wrap_content" android:gravity="center" /&gt; &lt;/LinearLayout&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