GridView layout in one of the most useful layouts in android. Gridview is mainly useful when we want show data in grid layout like displaying images or icons. This layout can be used to build applications like image viewer, audio or video players in order to show elements in grid manner.
Basic GridView Layout
1. Create a new project by going to File ⇒ New ⇒NewProject and fill required details. (I named my main activity as AndroidGridLayoutActivity.java)
2. Prepare your images which you want to show in grid layout and place them in res ⇒ drawable folder.
3. Create a new XML layout under layout and name it as grid_layout.xml (Right Click) layout ⇒ New ⇒ Android XML File.
grid_layout.xml
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/grid_view" android:nestedScrollingEnabled="true" android:layout_width="fill_parent" android:layout_height="wrap_content" android:numColumns="3" android:columnWidth="10dp" android:horizontalSpacing="50dp" android:verticalSpacing="5dp" android:gravity="center" android:stretchMode="columnWidth" > </GridView>
4. Create a new Class and name your class as ImageAdapter.java.
5. Extend your ImageAdapter.java class from BaseAdapter and fill it with following code.
ImageAdapter.java
package com.example.yashpwr; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; public class ImageAdapter extends BaseAdapter { private Context mContext; // Keep all Images in array public Integer[] mThumbIds = { R.drawable.pic_1, R.drawable.pic_2, R.drawable.pic_4, R.drawable.pic_4, R.drawable.pic_1, R.drawable.pic_4, R.drawable.pic_4, R.drawable.pic_2, R.drawable.pic_1, R.drawable.pic_4, R.drawable.pic_2, R.drawable.pic_4, R.drawable.pic_1, R.drawable.pic_4, R.drawable.pic_2 }; // Constructor public ImageAdapter(Context c){ mContext = c; } @Override public int getCount() { return mThumbIds.length; } @Override public Object getItem(int position) { return mThumbIds[position]; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(mContext); imageView.setImageResource(mThumbIds[position]); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(new GridView.LayoutParams(400, 500)); return imageView; } }
6. Open your MainActivity.java class as shown below.
MainActivity.java
package com.example.yashpwr; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.grid_layout); GridView gridView = (GridView) findViewById(R.id.grid_view); // Instance of ImageAdapter Class gridView.setAdapter(new ImageAdapter(this)); } }
Run your project
Showing selected grid image in new Activity (Full Screen)
Until now we displayed all images in simple grid layout. To this we can add functionality like showing selected image in fullscreen. For this we need to pass image resource id from grid view to new activity.
7. Create new XML file under layout folder and name it as full_image.xml and fill it following code.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/full_image_view" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
8. Create a new Activity and name your class as FullImageActivity.java and type following code.
FullImageActivity.java
package com.example.yashpwr; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.ImageView; public class FullImageActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.full_image); // get intent data Intent i = getIntent(); // Selected image id int position = i.getExtras().getInt("id"); ImageAdapter imageAdapter = new ImageAdapter(this); ImageView imageView = (ImageView) findViewById(R.id.full_image_view); imageView.setImageResource(imageAdapter.mThumbIds[position]); } }
9. Modify your MainActivity.java class to following. I added click event for grid element.
MainActivity.java
package com.example.yashpwr; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.grid_layout); GridView gridView = (GridView) findViewById(R.id.grid_view); // Instance of ImageAdapter Class gridView.setAdapter(new ImageAdapter(this)); /** * On Click event for Single Gridview Item * */ gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View v, int position, long id) { // Sending image id to FullScreenActivity Intent i = new Intent(getApplicationContext(), FullImageActivity.class); // passing array index i.putExtra("id", position); startActivity(i); } }); } }
10. Open your AndroidManifest.xml and add entries of newly added activity.AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yashpwr"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:hardwareAccelerated="false" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" android:largeHeap="true"> <activity android:name=".AndroidGridLayoutActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- FullImageActivity --> <activity android:name=".FullImageActivity"></activity> </application> </manifest>
Finally run your project
Happy Coding 🙂
Yash Pawar
Yash is hardcore programmer and programming has been his passion since he compiled his first hello-world program. Solving real problems of developers through tutorials has always been interesting part for him.