Open top menu


                   

             Android-Implement Image Gallery Wigets 

...................................................................................................................................................................

Stepe 1. create activity_main.xml res folder...

activity_main.xml
...................................................................................................................................................................

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"    
    android:orientation="vertical" >     
    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logoweb" />
    <ImageView
        android:id="@+id/image1"
        android:layout_width="215dp"
        android:layout_height="270dp"
        android:layout_gravity="center_horizontal"
        android:background="#cfcfcf"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:src="@drawable/logoweb" />
</LinearLayout>


Stepe 2. create atter.xml inside res/values/ folder...
...................................................................................................................................................................

<?xml version="1.0" encoding="utf-8"?>
<resources>
    
<declare-styleable name="MyGallery">
    
<attr name="android:galleryItemBackground" />

</declare-styleable>

</resources>



Stepe 3. create MainActivity.java inside src folder...
...................................................................................................................................................................
package com.androidbeginner.testcode;

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.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
 
Integer[] imageIDs = {
R.drawable.k,
R.drawable.l,
R.drawable.p,
R.drawable.t  
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle("Image Gallery example");
setContentView(R.layout.activity_main);
 
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position,long id)
{
Toast.makeText(getBaseContext(),"photo" + (position + 1) + " onClick",Toast.LENGTH_SHORT).show();  
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context contx)
{
context = contx;
 
TypedArray styl =obtainStyledAttributes(R.styleable.MyGallery);
itemBackground = styl.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
styl.recycle();
}
 
public int getCount() {
return imageIDs.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 imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}

try this code....

view More 

0 comments