Course Content

Course Content

Description

GridView in Android with Example

Now we will see an example of GridView in Android Studio. For that we will create a new project named 'Grid'.

Now we will design gridview, for that we will first open our manifest file 'AndroidManifest.xml',  find is default generated, we have not added any kind of new code to it.

Now we will design our project's layout, for that we will open 'activity_main.xml', in which we will create a gridview,

<Gridview

android:layout_height=""fill_parent""

android:layout_width=""fill_parent""

android:gravity=""center""

android:id=""@+id/gridview""

android:columnWidth=""90dp""

android:numColumns=""auto_fit""

android:verticalSpacing=""10dp""

android:horizontalSpacing=""10dp""

android:stretchMode=""columnWidth""   />

This is how we have set some basic properties for our project's layout and we can see this in our design view that how are gridview will be looking.

Now we want to design our 'java' folder, so first we will open 'MainActivity.java' file which is default generated, here we will now do gridview view identification,

GridView gridView=(GridView) findViewById(R.id.gridview);

 

Now we will create a class ImageAdapter, and then in 'ImageAdapter.java' we will code,

a

public class ImageAdapter extends BaseAdapter

{

private Context mContext;

public ImageAdapter(Context c)

{

mContext=c;

}

public Integer mThumbsIds=

{

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,

};

}

 

Now we will design a method,

public int getCount()

{

return mThumbIds.length;

}

 

We will define two specific items which we will need with an array,

public Object getItem(int position)

{

return null;

}             

public long getItemId(int position)

{

return 0;

}

 

Now we will create an ImageView, through which we will display the images which we have taken from the array with the help of an adapter in Gridview in Android,

public View getView(int position, View convertView, ViewGroup parent)

{

ImageView imageView;

 

Now on a condition basis, we will check that if our convertView is null then what should happen,

if (convertView==null)

{

imageView=new ImageView(mContext);

imageView.setLayoutParams(new GridView.LayoutParams(85,85));

imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

imageView.setPadding(10,10,10,10);

}

else

{

imageView.setImageResource(mThumbIds(position));

return imageView;

}

 

Now in our 'MainActivity.java' file, we need to add one more line of code, ie.,

GridView.setAdapter(new ImageAdapter (this));

 

Now we will execute our program, and after executing our program Gridview in Android is running successfully, and all the images are displayed in Gridview manner.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere