Monday, 25 March 2013

Gallery View Demo

Demo:

1)Activity:

package com.venky.galleryview;

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

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

      Gallery gallery;

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

            gallery = (Gallery) findViewById(R.id.gallery1);

            ImageAdapter ia = new ImageAdapter();
            gallery.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 imageView = new ImageView(getApplicationContext());

                  imageView.setLayoutParams(new Gallery.LayoutParams(
                              LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

                  imageView.setImageResource(images[position]);

                  return imageView;
            }

      }

      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" >

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


3)Download this Project Click Here
 

No comments:

Post a Comment