Monday, March 21, 2011

Gallary And Grid View In Android

The Following Application show how to Perform the Gallary and Grid view in android.

First Take a new Project and Name it as:GallaryAndGrid
And then create a android class "GalleryCustom" which extnds Activity

The Source Code for "GalleryCustom.java"  is like this



package com.gallrygrid;

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.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.gallrygrid.GalleryOne.ImageAdapter;


public class GalleryCustom extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.galleryone);
     // Reference the Gallery view
        Gallery g = (Gallery) findViewById(R.id.gallery);
        // Set the adapter to our custom adapter (below)
        g.setAdapter(new ImageAdapter(this));
       
        // Set a item click listener, and just Toast the clicked position
        g.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
               
                Toast.makeText(GalleryCustom.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }
    public class ImageAdapter extends BaseAdapter {
        int mGalleryItemBackground;
        private Context mContext;

       
        public ImageAdapter(Context c) {
            mContext = c;
            // See res/values/attrs.xml for the <declare-styleable> that defines
            // Gallery1.
          
        }

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

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            //LinearLayout ll = new LinearLayout(mContext);
            //LayoutParams lp;
           
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.gallerybox, null);
            }
            TextView tvname = (TextView)v.findViewById(R.id.txtviewbox);
            ImageView tvdetail = (ImageView)v.findViewById(R.id.imagebox);
           
            tvname.setText(mNames[position]);
            tvdetail.setImageResource(mImageIds[position]);
           
            //tvdetail.setScaleType(ImageView.ScaleType.FIT_XY);
            //tvdetail.setLayoutParams(new Gallery.LayoutParams(136, 88));
           
            return v;
        }

      
        private Integer[] mImageIds = {
                R.drawable.photo1,
                R.drawable.photo2,
                R.drawable.photo3,
                R.drawable.photo4,
                R.drawable.photo5,
                R.drawable.photo6,R.drawable.photo7
        };
        private String[] mNames = {
            "One", "Two","Three","Four","Five","Six"   
        };
    }
}



The "GalloryOne.xml" file is Like this

<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:spacing="3dip"
/>

Create An Activity in "AndroidManifeast.xml" like this

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gallrygrid"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GalleryOne"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

then this one Gives Out put as



No comments:

Post a Comment