Gridview in Android


package com.stress;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Mood extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mood);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(this));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Toast.makeText(Mood.this, "" + position, Toast.LENGTH_SHORT)
                        .show();

            }
        });
    }

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return null;
        }

        public long getItemId(int position) {
            return 0;
        }

        // create a new ImageView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            View grid;

            if (convertView == null) { // if it's not recycled, initialize some
                // attributes
                grid = new View(mContext);
                LayoutInflater inflater = getLayoutInflater();
                grid = inflater.inflate(R.layout.moodcus, parent, false);

            } else {
                grid = (View) convertView;

            }
            ImageView imageView = (ImageView) grid.findViewById(R.id.imgview);
            TextView textView = (TextView) grid.findViewById(R.id.txtview);
            imageView.setImageResource(mThumbIds[position]);
            textView.setText(mMoodnames[position]);
            return grid;
        }

        // references to our images
        private Integer[] mThumbIds = { R.drawable.angry_text,
                R.drawable.bored, R.drawable.calm, R.drawable.confident,
                R.drawable.confused, R.drawable.cool, R.drawable.dipressed,
                R.drawable.elated, R.drawable.embrassed, R.drawable.evil,
                R.drawable.flirt, R.drawable.forgetfull, R.drawable.frusted,
                R.drawable.happy, R.drawable.hungry, R.drawable.impatient,
                R.drawable.inlove, R.drawable.insecure, R.drawable.irrited,
                R.drawable.jealous, R.drawable.lonely, R.drawable.nervous,
                R.drawable.pure, R.drawable.sad, R.drawable.satisfied,
                R.drawable.scared, R.drawable.shy, R.drawable.sick,
                R.drawable.stressed, R.drawable.tired };

        private String[] mMoodnames = { "Angry", "Bored", "Calm", "Confident",
                "Confused", "Cool", "Dipressed", "Elated", "Embrassed", "Evil",
                "Flirt", "Forgetfull", "Frusted", "Happy", "Hungry",
                "Impatient", "Inlove", "Insecure", "Irrited", "Jealous",
                "Lonely", "Nervous", "Pure", "Sad", "Satisfied", "Scared",
                "Shy", "Sick", "Stressed", "Tired" };
    }

}


mood.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:background="@drawable/background">
    <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/grid">
        <GridView android:id="@+id/gridview" android:layout_width="fill_parent"
            android:scrollbars="none" android:layout_height="wrap_content"
            android:columnWidth="90dp" android:numColumns="4" android:fadingEdge="none"
            android:verticalSpacing="10dp" android:horizontalSpacing="10dp"
            android:stretchMode="columnWidth" android:gravity="center"
            android:layout_above="@+id/doneLay" android:layout_marginBottom="100dip" />
    </RelativeLayout>
    <RelativeLayout android:layout_width="fill_parent"
        android:id="@+id/doneLay" android:layout_below="@+id/grid"
        android:layout_centerHorizontal="true" android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button android:layout_width="wrap_content" android:id="@+id/done"
            android:layout_weight="1" android:layout_marginTop="10dip"
            android:layout_centerHorizontal="true" android:layout_marginBottom="55dip"
            android:layout_marginLeft="15dip" android:background="@drawable/done"
            android:layout_height="wrap_content" android:text="@string/done"></Button>
    </RelativeLayout>

</RelativeLayout>



moodcus.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:orientation="vertical">
    <ImageView android:id="@+id/imgview" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_gravity="center" />
    <TextView android:id="@+id/txtview" android:layout_width="wrap_content"
        android:textColor="#6687B2" android:layout_height="wrap_content"
        android:layout_gravity="center" />
</LinearLayout>