Sunday, September 1, 2013

GridView Example


GridView is a 2 - Dimensional display of your content. It is mainly useful to show icons or images.
Take a look of GridView.



Example:
  • First Create project named "GridView_Example".
  • Download some images/photos you would like to set in GridView and save it in res/drawable/ directory.
  • Open activity_main.xml file and add following code:

activity_main.xml



<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>


  • Open the MainActivity.java file and add following code:

MainActivity.java



public void onCreate(Bundle savedInstanceState) 
{
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   GridView gridview = (GridView) findViewById(R.id.gridview);
   gridview.setAdapter(new ImageAdapter(this));

   gridview.setOnItemClickListener(new OnItemClickListener() {
       public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
       {
           Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
       }
   });
}


  • Now You get some error in MainActivity.java file,but ignore it.Create New Class file (File - New - Class) named it ImageAdapter.java.
  • Insert following code in ImageAdapter.java file. 

ImageAdapter.java



public class ImageAdapter extends BaseAdapter
{
private Context mContext;

    public ImageAdapter(Context c) 
    {
        mContext = c;
    }

    public int getCount() 
    {
        return mThumbIds.length;
    }

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) 
        {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } 
        else 
        {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.cat, R.drawable.dog,
            R.drawable.cat_two, R.drawable.dog_two,
            R.drawable.dog_three, R.drawable.doglast,
            R.drawable.cat, R.drawable.dog,
            R.drawable.cat_two, R.drawable.dog_two,
            R.drawable.dog_three, R.drawable.doglast,
            R.drawable.cat, R.drawable.dog,
            R.drawable.cat_two, R.drawable.dog_two,
            R.drawable.dog_three, R.drawable.doglast,
            R.drawable.cat, R.drawable.dog,
            R.drawable.cat_two, R.drawable.dog_two,
            R.drawable.dog_three, R.drawable.doglast,
    };

}

  • Note that, here you have to change names in mThumbIds integer array as per your image names.
  • Done, Now run the project and get output like above image.  


No comments:

Post a Comment