Wednesday, August 20, 2014

Android Gallery Images using dots

1 comment :
Gallery Images using dots in Android :

        This is an Example how to create dots while scrolling the images in Android


1. Create a Android Application project

2.After that create drawable folder under res and put all the images

3.create the ImageAdapter class that extends the BaseAdapter that implements all predefined methods and also create an constructor array of integers which storing imageids

ImageAdapter.java:

package com.example.galleryimage;

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 IamgeAdapter(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.images2, R.drawable.img1, R.drawable.img2};
    }

4.Then put these codings in MainActivity.java

MainActivity.java :

package com.example.galleryimage;

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.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

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

@Override
protected 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 IamgeAdapter(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++) {
                   MainActivity.page_text[i].setTextColor(android.graphics.Color.GRAY);
                    }
                 MainActivity.page_text[position].setTextColor(android.graphics.Color.WHITE);
               
            }

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

}


5.put these code in activity_main.xml

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gallery_paging"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <Gallery
        android:id="@+id/mygallery01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

    <LinearLayout
        android:id="@+id/image_count"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal"
        android:gravity="center" >
    </LinearLayout>

</RelativeLayout>


Screenshots:







DOWNLOAD CODE


1 comment :