Gallery View

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:orientation="vertical" >

    <Gallery
        android:id="@+id/Gallery01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </Gallery>

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ImageView>

</LinearLayout>



GalleryView.java


package moor.android.gallery;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class GalleryView extends Activity {
    Integer[] pics = {
            R.drawable.antartica1,
            R.drawable.antartica2,
            R.drawable.antartica3,
            R.drawable.antartica4,
            R.drawable.antartica5,
            R.drawable.antartica6,
            R.drawable.antartica7,
            R.drawable.antartica8,
            R.drawable.antartica9,
            R.drawable.antartica10
    };
    ImageView imageView;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Gallery ga = (Gallery)findViewById(R.id.Gallery01);
        ga.setAdapter(new ImageAdapter(this));
       
        imageView = (ImageView)findViewById(R.id.ImageView01);
        ga.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getBaseContext(),
                        "You have selected picture " + (arg2+1) + " of Antartica",
                        Toast.LENGTH_SHORT).show();
                imageView.setImageResource(pics[arg2]);
              
            }
          
        });
       
    }
   
   
    public class ImageAdapter extends BaseAdapter {

        private Context ctx;
        int imageBackground;
      
        public ImageAdapter(Context c) {
            ctx = c;
            TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
            imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
            ta.recycle();
        }

        @Override
        public int getCount() {
          
            return pics.length;
        }

        @Override
        public Object getItem(int arg0) {
          
            return arg0;
        }

        @Override
        public long getItemId(int arg0) {
          
            return arg0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            ImageView iv = new ImageView(ctx);
            iv.setImageResource(pics[arg0]);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setLayoutParams(new Gallery.LayoutParams(150,120));
            iv.setBackgroundResource(imageBackground);
            return iv;
        }

    }
}


Screen shot :


 


14 comments:

  1. http://saigeethamn.blogspot.in/2010/05/gallery-view-android-developer-tutorial.html

    ReplyDelete
  2. Gallery is deprecrated in API 16..:(

    But nice tutorial

    ReplyDelete
  3. It is a very nice tutorial but could you show some simple gallery app for images which are saved in external storage?

    ReplyDelete
  4. can u tell me plz how to make button to set wallpaper in above code??

    ReplyDelete
  5. could you show some simple gallery app for images which are located in external like /sdcard/san/photos/

    ReplyDelete
  6. Please full describe where you found styleable

    ReplyDelete
    Replies
    1. to create resources.xml file in res/values..and then type.





      Delete
  7. this tutorial is taken from somewhere else !!! copy huhh

    ReplyDelete
  8. Android Gallery View for kitkat 4.4 http://www.androidinterview.com/android-gallery-view-example-displaying-a-list-of-images/

    ReplyDelete
  9. Thank,Thank.....Thank,Thank.....
    Thank,Thank.....Thank,Thank.....Thank,Thank.....Thank,Thank.....
    Thank,Thank.....Thank,Thank.....Thank,Thank.....Thank,Thank.....Thank,Thank.....

    ReplyDelete
  10. Nice tutorial.

    You can also follow my blogs.
    emergingandroidtech.blogspot.in

    Thank you.

    ReplyDelete
  11. thanks , "thats really wonderful ",am trying do that with python but still have problem
    anyone can help?

    ReplyDelete
  12. how can i use this example from gallary images not from drawable folder images.
    Means make it dynamic.

    ReplyDelete