Pages

Chapter 9: Gridview in Android

GridView 

In this chapter we will learn to implement the gridview with a simple example in Android Studio. Gridview is a viewgroup which displays the items in a two dimensional grids which is inserted through ListAdapter. 

Example:
  • Create a new project and name it as GridViewExample.
  • Design a Gridview in main layout.
File: activity_main.xml:
<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"
    android:padding="16dp"
    tools:context="com.example.hss_24.gridviewexample.MainActivity">

    <GridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center">

    </GridView>

</RelativeLayout>
  • Create a new layout as grid_cell.xml and design an ImageView in it.
File: grid_cell.xml:
<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/image"
        android:layout_width="100dp"
        android:layout_height="100dp" />

</LinearLayout>
  • Code the following things in MainActivity.
  • Create an integer array to store images.
  • Create a Base Adapter and set it in gridview.
  • Inflate the grid_cell layout.
File: MainActivity.java:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    //  Declare integer array
    int[] images = {R.drawable.cupcake, R.drawable.donut, R.drawable.eclair, R.drawable.froyo, R.drawable.gingerbread,
            R.drawable.honeycomb, R.drawable.icecream_sandwich, R.drawable.jellybean, R.drawable.kitkat,
            R.drawable.lollipop, R.drawable.marsh_mallow};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //  Gridview element and set the adapter
        GridView gridView = (GridView) findViewById(R.id.grid_view);
        gridView.setAdapter(new MyAdapter());
    }

    public class MyAdapter extends BaseAdapter {
        public MyAdapter() {
            super();
        }

        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            //  Inflating the layout
            LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
            convertView = inflater.inflate(R.layout.grid_cell, null);

            ImageView imageView = (ImageView) convertView.findViewById(R.id.image);

            //  Set images into imageview
            imageView.setImageResource(images[position]);
            return convertView;
        }
    }
}

  • Run the Application and enjoy the output.
Output:

Download project here
Thank You!!!
Please like and share...



Unknown

Author: Nouman Baig

Editor: Mohammad Jabir

No comments:

Post a Comment