Hi Friends, if you like my blog please give your valuable comments it will help to improve my blog content and enthusiasm to write a lot in android World.

Thursday, March 1, 2012

Android Gallery ImageView Example

Now we are going to see a simple gallery example of how to use gallery look like a photo album as like in our phone. That is, when we click the item in gallery, the corresponding image will display below in full size using imageview.
Create a attrs.xml file in res/values folder. This file is used to declare the style. ( Code in previous post - simple gallery example).
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
 android:layout_width="fill_parent" android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical">
 <Gallery xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/examplegallery" android:layout_width="fill_parent"
  android:layout_height="wrap_content" />
 <ImageView android:id="@+id/ImageView01"
  android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>
Java file
public class GalleryExample extends Activity {
    private Gallery gallery;
    private ImageView imgView;
private Integer[] Imgid = {
            R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
    };
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
imgView = (ImageView)findViewById(R.id.ImageView01);
        imgView.setImageResource(Imgid[0]);
gallery = (Gallery) findViewById(R.id.examplegallery);
         gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
                imgView.setImageResource(Imgid[position]);
            }
        });
}
public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;
public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
        }
public int getCount() {
            return Imgid.length;
        }
public Object getItem(int position) {
            return position;
        }
public long getItemId(int position) {
            return position;
        }
public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
            imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);
return imgView;
        }
    }}
 

No comments:

Post a Comment