Android GridView Example

GridView is a main useful layout of android.We can store our images,videos,or icons in GridView items that  items is display as a two-dimensional, scrollable grid.

To set the values in a GridView we are using a BaseAdapter.

To get the position of the items in a GridView we are using gridview.setOnItemClickListener().

You can download source code here.

1.Create a new project named GridViewDemo.
2.Download sample images. Save the image files into the project's res/drawable.
3.Open the res/layout/activity_grid_view.xml file and insert the following:

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

4.Open GridViewActivity.java and insert the following code:

package com.etr.gridviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;

public class GridViewActivity extends Activity {
GridView view;

@Override
protected void onCreate(Bundle savedInstanceState) {
try {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_view);

view = (GridView) findViewById(R.id.grid);
view.setAdapter(new MyAdapter(this));

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

} catch (Exception e) {
Log.v("Exception", Log.getStackTraceString(e));
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.grid_view, menu);
return true;
}

}

5.Open MyAdapter.java and insert the following code:

package com.etr.gridviewdemo;

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 MyAdapter extends BaseAdapter {

Context context;

public MyAdapter(Context con) {
context = con;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return 30;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}

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

ImageView imageView;

if (convertView == null) {

imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(80, 80));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);

} else {

imageView = (ImageView) convertView;

}

imageView.setImageResource(R.drawable.img);
return imageView;
}

}

Output Screen:


Comments

Post a Comment

Popular posts from this blog

SQLiteDatabase With Multiple Tables

Programmatically turn ON/OFF WiFi on Android device

Android Service and IntentService