Code
Packagecom.lxt008;ImportCOM.LXT008.R;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.AdapterView;ImportAndroid.widget.GridView;ImportAndroid.widget.Toast;ImportAndroid.widget.AdapterView.OnItemClickListener; Public classActivity01extendsactivity{/**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); //get the GridView objectGridView GridView =(GridView) Findviewbyid (R.id.gridview); //add element to GridViewGridview.setadapter (NewImageadapter ( This)); //setting the background of a galleryGridview.setbackgroundresource (R.DRAWABLE.BG0); //Event MonitoringGridview.setonitemclicklistener (NewOnitemclicklistener () { Public voidOnitemclick (adapterview<?> parent, View V,intPositionLongID) {toast.maketext (Activity01. This, "You have selected" + (position + 1) + "number Picture", Toast.length_short). Show (); } }); }}
Layout file
<?XML version= "1.0" encoding= "Utf-8"?><GridViewxmlns:android= "Http://schemas.android.com/apk/res/android"Android:id= "@+id/gridview"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:numcolumns= "Auto_fit"android:verticalspacing= "10DP"android:horizontalspacing= "10DP"Android:columnwidth= "90DP"Android:stretchmode= "ColumnWidth"android:gravity= "Center"/>
Android-gridview Grid trying to Demo