In res/layout/main.xml layout add GridView as :
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/grdview"
android:paddingTop="20dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="40dp"
android:horizontalSpacing="30dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
In res/layout/ Create an gridviewitem.xml layout for GridView items as :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:paddingBottom="5dip"
android:layout_height="fill_parent" >
<ImageView
android:layout_height="wrap_content"
android:id="@+id/grditemimg"
android:layout_width="wrap_content"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_below="@+id/grditemimg"
android:layout_height="wrap_content"
android:text="TextView01"
android:layout_centerHorizontal="true"
android:id="@+id/grditemtxt">
</TextView>
</RelativeLayout>
In GridViewActivity :
public class GridViewActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.
FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.grdview);
ArrayList<HashMap<String, Object>> lstImageItem =
new ArrayList<HashMap<String, Object>>();
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("itemimg", R.drawable.v01);
map.put("itemtxt", "Blogger");
lstImageItem.add(map);
map = new HashMap();
map.put("itemimg", R.drawable.v02);
map.put("itemtxt", "Facebook");
lstImageItem.add(map);
map = new HashMap();
map.put("itemimg", R.drawable.v03);
map.put("itemtxt", "Flickr");
lstImageItem.add(map);
map = new HashMap();
map.put("itemimg", R.drawable.v04);
map.put("itemtxt", "Hi5");
lstImageItem.add(map);
map = new HashMap();
map.put("itemimg", R.drawable.v05);
map.put("itemtxt", "Linkedin");
lstImageItem.add(map);
map = new HashMap();
map.put("itemimg", R.drawable.v06);
map.put("itemtxt", "Netvibes");
lstImageItem.add(map);
SimpleAdapter saImageItems = new SimpleAdapter(this,
lstImageItem,
R.layout.gridviewitem,
new String[] {"itemimg","itemtxt"},
new int[] {R.id.grditemimg,R.id.grditemtxt});
gridview.setAdapter(saImageItems);
gridview.setOnItemClickListener(new ItemClickListener());
}
class ItemClickListener implements OnItemClickListener
{
@SuppressWarnings("unchecked")
@Override
public void onItemClick(AdapterView arg0,
View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
HashMap<String, Object> item=
(HashMap<String, Object>) arg0.getItemAtPosition(arg2);
Toast.makeText(GridViewActivity.this,
(String)item.get("itemtxt"), Toast.LENGTH_SHORT).show();
setTitle((String)item.get("itemtxt"));
}
}
}
Download Source Code: GridViewExample






0 comments:
Post a Comment