Thursday, 22 November 2012

Gallery with pagging dots

main.xml

         <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res       /android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#000000"
        android:id="@+id/gallery_paging"
        >
           <Gallery android:id="@+id/mygallery01"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:spacing="0px"
                android:fadingEdge="none"/>
             <LinearLayout android:id="@+id/image_count"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"          
                android:gravity="center"         
                android:layout_alignParentTop="true" >  
            </LinearLayout>          

</RelativeLayout>
ImageAdapter.java

       package com.gallerWithDotPagging;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class ImageAdapter extends BaseAdapter {
    private Context context;

    public ImageAdapter(Context c) {
    // TODO Auto-generated constructor stub
    this.context = c;
    }
    public int getCount() {
    // TODO Auto-generated method stub
    return flowers.length;
    }
    public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
    }
    public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView image = new ImageView(context);
    image.setImageResource(flowers[position]);
    image.setScaleType(ScaleType.FIT_XY);
    // image.setLayoutParams(new Gallery.LayoutParams(400, 400));
    return image;
    }
    int[] flowers = { R.drawable.image1, R.drawable.image2,
    R.drawable.image3,R.drawable.image4, R.drawable.image5};
    }

 GalleryActivity.java


    package com.gallerWithDotPagging;

import com.gallerWithDotPagging.R;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GalleryActivity extends Activity {
    Gallery gallery;
    ImageAdapter imageAdapter;
    LinearLayout count_layout;
    int count = 0;
    static TextView page_text[];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        count_layout = (LinearLayout) findViewById(R.id.image_count);
        gallery = (Gallery) findViewById(R.id.mygallery01);
        imageAdapter = new ImageAdapter(this);
        gallery.setAdapter(imageAdapter);
        count=gallery.getAdapter().getCount();
        page_text = new TextView[count];
        for (int i = 0; i < count; i++) {
            page_text[i] = new TextView(this);
            page_text[i].setText(".");
            page_text[i].setTextSize(45);
            page_text[i].setTypeface(null, Typeface.BOLD);
            page_text[i].setTextColor(android.graphics.Color.GRAY);
            count_layout.addView(page_text[i]);
        }
        gallery.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                // TODO Auto-generated method stub
                 for (int i = 0; i < count; i++) {
                   GalleryActivity.page_text[i].setTextColor(android.graphics.Color.GRAY);
                    }
                 GalleryActivity.page_text[position].setTextColor(android.graphics.Color.WHITE);
               
            }

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


Output:-




1 comment:

  1. thanks for the nice tutorial..

    can you pls tell me how to put another gallery into the same activity with horizontal scroll.

    ReplyDelete