Saturday, June 30, 2012

Display photo when it is selected in Gallery using Android application

Gallery Example
Example show: Photo get display when it is selected in Gallery.
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon"></ImageView>
<Gallery android:id="@+id/gallery1" android:layout_width="match_parent" android:layout_height="wrap_content"></Gallery>
</LinearLayout>
For Background
attributes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="Gallery1">
 <attr name="android:galleryItemBackground"></attr>
 </declare-styleable>   
</resources>
Now moving in coding part.

package com.galleryexample;


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 GallaryExampleActivity extends Activity {
    /** Called when the activity is first created. */
Integer[] pics={
        R.drawable.inv1,
        R.drawable.inv2,
        R.drawable.inv3,
        R.drawable.inv4,
        R.drawable.inv5,
        R.drawable.inv6,
        R.drawable.inv7,
        R.drawable.inv8,
        R.drawable.inv9
       
      };
       ImageView imgview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Gallery ga=(Gallery)findViewById(R.id.gallery1);
    ga.setAdapter(new ImageAdapter(this));
    imgview =(ImageView)findViewById(R.id.imageView1);
       
       // display image when photo is selected in gallery
   ga.setOnItemSelectedListener(new              Gallery.OnItemSelectedListener() {


     @Override
     public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

     Toast.makeText(getBaseContext(), "You have selected picture " + (arg2+1) + "", Toast.LENGTH_SHORT).show();
     imgview.setImageResource(pics[arg2]);
    }


    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }
    });
       
   //display image when photo is clicked in gallery
/*  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) + "", Toast.LENGTH_SHORT).show();
     imgview.setImageResource(pics[arg2]);
    }
    }); */
}


//creating another class
    
    public class ImageAdapter extends BaseAdapter {
    private Context ctx;
    int imgback;
    public ImageAdapter(Context c)
    {
    ctx=c;
    TypedArray ta=c.obtainStyledAttributes(R.styleable.Gallery1);
    imgback=ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
    ta.recycle();
    }


    @Override
    public int getCount() {
    // TODO Auto-generated method stub
    return pics.length ;
    }


    @Override
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
    }


    @Override
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView iv=new ImageView(ctx);
    iv.setImageResource(pics[position]);
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setLayoutParams(new Gallery.LayoutParams(150,150));
    iv.setBackgroundResource(imgback);
    return iv;
    }
     }
}

output

No comments:

Post a Comment