Saturday 24 May 2014

Image-Gallery-Android-Example

ANDROID IMAGE GALLERY

ImageGallery : image gallery is use to show images in sequence. it found in transition.

Create an android project.
step 1 :
          open res/layout/activity_main.xml and write following code.
Layout xml file code: 



    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

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


Step 2: create attrs.xml file under res/values folder.

   
       
   

Step 3: paste some images under res/drawable folder.


Now your application directory structure like this.
android-gallery-shivendra

step 4:- java source code 

package com.example.imgproject;

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

public class MainActivity extends Activity {

int imglist[] = {R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d,R.drawable.e};
Gallery gal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gal = (Gallery)findViewById(R.id.gallery1);
ImgAdp iadp = new ImgAdp(MainActivity.this);
gal.setAdapter(iadp);
gal.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView arg0, View arg1, int pos,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "img "+pos+"click", Toast.LENGTH_LONG).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ImgAdp extends BaseAdapter
{
Context mcon;
int itemBackground;

public ImgAdp(Context cont)
{
mcon =cont;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);

            itemBackground = a.getResourceId(
                R.styleable.Gallery1_android_galleryItemBackground, 0);        
            a.recycle();

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView;
            if (convertView == null) {
            imageView = new ImageView(mcon);
                imageView.setImageResource(imglist[position]);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));                
            }
            else 
            {
            imageView = (ImageView) convertView;
            }            
            imageView.setBackgroundResource(itemBackground);
            return imageView;

}
}

}

Now test your application.

Download Complete Example


Today's Pageviews