Android GridView Example Tutorial

Insert multiple images in GridView using string url array dynamically.

GridView is a type of 2-d image showing view basically used to display images on android mobile activity screen. GridView display images into Thumbnail view because it can show multiple images at single time with automatically scroll view inserted ability. On my this tutorial i am showing images on GridView using string images hold url array and with the use of list adapter inserting the images into grid view format. So here is the complete step by step tutorial for Android GridView Example Tutorial.

android-project-download-code-button

Android GridView Example Tutorial

Android GridView Example Tutorial.

Put all the images you want to show on gridView in drawable-hdpi folder.

paste images here
Code for MainActivity.java file.

 package com.android_examples.com.gridviewandroid;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity {

 // ALL IMAGES drawable-hdpi folder path with image name.
 Integer[] DisplayImagesList = {R.drawable.one, R.drawable.two, R.drawable.three, R.drawable.four, R.drawable.five
 , R.drawable.six};
 GridView GridViewDisplay;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) 
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 GridViewDisplay = (GridView)findViewById(R.id.gridView1);
 
 // Setting up Image adapter on gridview.
 GridViewDisplay.setAdapter(new ImageAdapter(this));
 }

 private class ImageAdapter extends BaseAdapter
 {
 Context context;
 
 public ImageAdapter(Context context) 
 {
 this.context = context;
 }
 
 @Override
 public int getCount() 
 {
 return DisplayImagesList.length;
 }

 @Override
 public Object getItem(int position) 
 {
 return DisplayImagesList[position];
 }

 @Override
 public long getItemId(int position) 
 {
 return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) 
 {
 //Create new image view on GridView to display images.
 
 ImageView imageView = new ImageView(this.context);
 
 //Setting up images position so images can call one by one. 
 imageView.setImageResource(DisplayImagesList[position]);
 imageView.setLayoutParams(new GridView.LayoutParams(200, 150));
 
 return imageView;
 }
 }
 }

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context="com.android_examples.com.gridviewandroid.MainActivity" >

 <GridView
 android:id="@+id/gridView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true"
 android:numColumns="3" >
 </GridView>

</RelativeLayout>

Screenshot:

gridview Including Images

Click Here To Download Android GridView Example Tutorial project.

Leave a Reply

Your email address will not be published. Required fields are marked *