Monday 25 March 2013

GridView Demo



Demo:
 
Activity:
package com.venky.gridview;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewActivity extends Activity {
      /** Called when the activity is first created. */

      GridView gridView;

      @Override
      public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            gridView = (GridView) findViewById(R.id.gridView1);
            ImageAdapter ia = new ImageAdapter();

            gridView.setAdapter(ia);

      }

      class ImageAdapter extends BaseAdapter {

            public int getCount() {
                  // TODO Auto-generated method stub
                  return images.length;
            }

            public Object getItem(int position) {
                  // TODO Auto-generated method stub
                  return null;
            }

            public long getItemId(int position) {
                  // TODO Auto-generated method stub
                  return 0;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                  // TODO Auto-generated method stub

                  ImageView image = new ImageView(getApplicationContext());

                  image.setLayoutParams(new GridView.LayoutParams(100, 100));

                  image.setImageResource(images[position]);

                  return image;
            }

      }

      int[] images = { R.drawable.a1, R.drawable.a2, R.drawable.a3,
                  R.drawable.a4, R.drawable.a5, R.drawable.a6, R.drawable.a7,
                  R.drawable.a8, R.drawable.a9, R.drawable.a10, R.drawable.a11,
                  R.drawable.a12 };
}


2)main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="3" >
    </GridView>

</LinearLayout>


3)Download this Project Click Here
 

No comments:

Post a Comment