GridView
GridView layout is used to display data in two dimensional grid. It is a subclass of AdapterView. You can bind data from an external source to the GridView only by using an Adapter. Adapter acts as a bridge between the UI components and data source. It simply retrieves and fills the data to the UI components like gridview, listview or spinner.
Attributes:
1) numColumns: defines the number of columns to be displayed in a grid. You can assign a value "auto_fit" which displays as many columns as possible to fill the available space.
2) columnWidth: It specifies the fixed width for each column.
3) gravity: It specifies the gravity/ alignment within each cell.
4) stretchMode: You can specify certain values for this attribute as shown below
none: stretching is disabled
spacingWidth: The spacing between each column is stretched.
columnWidth: Each column is stretched equally.
spacingWidthUniform: The spacing between each column is uniformly stretched.
5) padding: It adds space between content and border i.e. padding top, bottom, right and left.
6) margin: It adds space between border and its parent layout. Like padding, adds space on top, bottom, left and right.
7) horizontalSpacing: It adds space between cells in each row or say spacing between each column (horizontally). This could be in px, dp, sp, in or mm.
8) verticalSpacing: It adds space between cells in each column or say spacing between each row (vertically). This could be in px, dp, sp, in or mm.
9) scrollbars: It defines which scrollbar should be displayed on scrolling or not. i.e. horizontal or vertical or none.
10) background: It sets a drawable background or specific background color for the view.
You can get or set the values of each attribute at runtime. Some of the get and set methods have been defined as follows:
Get Methods
public int getNumColumns()
public int getColumnWidth()
public int getGravity()
public int getHorizontalSapcing()
public int getVerticalSpacing()
public int getStretchMode()
public ListAdapter getAdapter()
Set Methods
public void setNumColumns(int numColumns)
public void setColumnWidth(int columnWidth)
public int getGravity(int Gravity)
public int getHorizontalSapcing(int horizontalSpacing)
public int getVerticalSpacing(int verticalSpacing)
public int getStretchMode(int stretchMode)
public ListAdapter getAdapter(ListAdapter adapter)
To load String array using ArrayAdapter:
· ArrayAdapter fits in between an ArrayList (data source) and the GridView (visual representation) and configures two aspects: (i) Which array to use as the data source for the grid; (ii) How to convert any given item in the array into a corresponding View object.
·ArrayAdapter arr = new ArrayAdapter(Context c, int Resource, String[] objects);
where,
Context defines the activity instance.
Resource defines the XML layout to be used.
objects defines the array of data.
Example:
gridview_layout.xml
<?xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/lGridSimple" android:numColumns="auto_fit" android:columnWidth="100dp" android:gravity="center" android:stretchMode="columnWidth"> </GridView>
Gridview_code.java
public class GridView_code extends Activity {
GridView grid_alpha; static final String[] numbers = new String[]{ "A", "B", "C", "D","E", "F", "G", "H", "I", "J", "K", "L","M","N","O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gridview_layout); grid_alpha = (GridView) findViewById(R.id.lGridSimple); ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android .R.layout.simple_list_item_1, numbers); grid_alpha.setAdapter(adapter);
grid_alpha.setOnItemClickListener(new AdapterView.OnItemClickListener () { @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(getApplicationContext(),"You have pressed " + ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); } }); } }
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.admin.androidstuff" > <application android:allowBackup="true" android:label="@string/app_name"
tools:ignore="NewApi"> <activity android:name=".Gridview_code" android:label="@string/app_na me"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>














