25 Dec 2012


How To Create Grid View In Android

GridView is a ViewGroup which provides two-dimensional and scrollable view to the user.Grid view has some important points:
  • GridView is automatically inserted in the layout using  ListAdapter(a bridge between ListView and data which backs the list).
  • android.widget.GridView is the class which needs to import for implementing the grid view.

so lets us learn how to create grid view.
  • first start the eclipse and select new-> android application project.
Grid View 1

  • Now fill the following details in the android application project interface.
    • Application Name- GridView
    • Project Name-GridView
    • Package Name-grid.view
    • and then click next ->next and then finish.

Grid View 2
  • Now in the project GridView open the activity_main.xml file under res-> layout-> activity_main.xml and replace the whole lines with these new lines.
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
     android:layout_width="fill_parent"  
     android:layout_height="fill_parent">  
     <GridView  
       android:id="@+id/gridView1"  
       android:layout_width="match_parent"  
       android:layout_height="match_parent"  
       android:background="#aabbcc"  
       android:numColumns="2"  
       android:verticalSpacing="5dp" >  
     </GridView>  
   </LinearLayout>  
  • Here we have added GridView inside the Linear Layout .and we have set the maximum col for menu item is two.
  • Now open the MainActivity.java and add few lines of java so that it finally looks like this:
 package grid.view;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.widget.ArrayAdapter;  
 import android.widget.GridView;  
 public class MainActivity extends Activity {  
       GridView gridView;  
    String[] weekday={"SUN","MON","TUE","THU","FRI","SAT","SUN"};  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     gridView = (GridView)findViewById(R.id.gridView1);  
     ArrayAdapter<String> adapterview= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,weekday);  
     gridView.setAdapter(adapterview);  
   }  
  }  
  • Now lets understand the code of the MainActicity.java:
    • import android.widget.ArrayAdapter is a concrete BaseAdapter which is backed by  arbitrary adapter.
    • import android.widget.GridView is class that is used for showing two dimensional grid objects.
    • setAdapter() method sets the custom adapter for source of all item for displaying in the grid.
    • simple_list_item_1 is constant of android.R.layout which is able to change its background state to indicate when it is acticated.
  • Now our project is ready therefore we will run this project by right click on the project folder and click on run.
Grid View 3

  • Now when you put your cursor and click on each item in the grid it will change it color although it will not perform any task as we haven't assigned any function with these item.

No comments:

Post a Comment

Translate