Sunday, December 16, 2012

Using GridView in android

Creation of a grid and showing a number of icons/images and their details is simple and easy to understand. we can achieve these all in few simple steps:)
 Think about it that we need to show a group of country icons and the names of those countries.
to achieve this firstly we need to make a LinearLayout where the orientation is vertical and there is an image icon of country flag ,with its corresponding TextView to show the name of the respective country.

1) write gridview_layout.xml  file as follows


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"  
    android:layout_gravity="center"  
     >
     
    <ImageView 
       android:id="@+id/flag"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:padding="10dp"       
       
       />
    
        
<TextView 
android:id="@+id/txt"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:textSize="15dp"
   android:gravity="center_horizontal"        
   
   />
       

</LinearLayout>

2) write activity_main.xml as follows:

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/gridview"
    android:numColumns="auto_fit"
 />


3) use the above in MainActivity.java as follows


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

  // Array of strings storing country names
    String[] countries = new String[] {
            "India",
            "Pakistan",
            "Sri Lanka",
            "China",
            "Bangladesh",
            "Nepal"
            
    };

    // Array of integers points to images stored in /res/drawable-ldpi/
    int[] flags = new int[]{
                R.drawable.india,
                R.drawable.pakistan,
                R.drawable.srilanka,
                R.drawable.china,
                R.drawable.bangladesh,
                R.drawable.nepal
               
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

      //Remove notification bar
    
        setContentView(R.layout.activity_main);
        
        
     // Each row in the list stores country name, currency and flag
        List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

        for(int i=0;i<6;i++){
            HashMap<String, String> hm = new HashMap<String,String>();
            hm.put("txt", countries[i]);            
            hm.put("flag", Integer.toString(flags[i]) );
            aList.add(hm);
        }

        // Keys used in Hashmap
        String[] from = { "flag","txt"};

        // Ids of views in listview_layout
        int[] to = { R.id.flag,R.id.txt};

        // Instantiating an adapter to store each items
        // R.layout.listview_layout defines the layout of each item
        SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.gridview_layout, from, to);        
        
        // Getting a reference to gridview of MainActivity
        GridView gridView = (GridView) findViewById(R.id.gridview);
        
        // Setting an adapter containing images to the gridview
        gridView.setAdapter(adapter);
        
        
    }

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



Have a great day with Android coding :)

No comments:

Post a Comment