Program to show gallery of images using BaseAdapter in android
 
package com.chitraksh.tutorial.chilearn4;

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

public class Gallery extends Activity {
    //---the images to display---
    Integer[] imagesIDs={
            R.drawable.manufacturer_fox,
            R.drawable.small10,
            R.drawable.small11,
            R.drawable.small12,
            R.drawable.small13,
            R.drawable.small8,
            R.drawable.small9,
            R.drawable.travelmap,
            R.drawable.wildlife
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gallery);

        initilizeActivity();
        
        android.widget.Gallery gallery =(android.widget.Gallery)findViewById(R.id.gallery1);
        gallery.setAdapter(new MyImageAdapter(this));
        gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(),"pic "+(position+1) + " selected",Toast.LENGTH_SHORT).show();
                ImageView imageView=(ImageView)findViewById(R.id.image1);
                imageView.setImageResource(imagesIDs[position]);
            }
        });
    }
    private void initilizeActivity()
    {
    	ActionBar action=getActionBar();
        action.setDisplayHomeAsUpEnabled(true);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	if(item.getItemId()==android.R.id.home)
    	{
    		finish();
    	}
    	return super.onOptionsItemSelected(item);
    }
    private  class  MyImageAdapter extends BaseAdapter
    {
        Context context;
        int itemBackgorund;
        public  MyImageAdapter(Context c)
        {
            this.context=c;
            //---setting the style---
            TypedArray a=obtainStyledAttributes(R.styleable.Gallery1);
            itemBackgorund=a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
            a.recycle();
        }
        //---returns the number of images---
        public  int getCount()
        {
            return  imagesIDs.length;
        }

        @Override
        public Object getItem(int position) {
            return imagesIDs[position];
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if(convertView==null)
            {
                imageView=new ImageView(context);
                imageView.setImageResource(imagesIDs[position]);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                imageView.setLayoutParams(new android.widget.Gallery.LayoutParams(150, 120));
            }
            else
            {
                imageView=(ImageView)convertView;
            }
            imageView.setBackgroundResource(itemBackgorund);
            return  imageView;
        }
    }
}
share on whatapp
183 Views

Comments

106.215.139.125what is custom adapter in android please guide in detail
 
 
Private Policy   Terms of Service   4922