Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use Gallery for scrolling images with page indicator in android

    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.87k
    Comment on it

    First of all you need to use a custom class in place of Gallery :-

    package com.pakagename;

    import android.content.Context;

    import android.util.AttributeSet;

    import android.view.KeyEvent;

    import android.view.MotionEvent;

    import android.widget.Gallery;

    public class ExtendedGallery extends Gallery{

    private boolean stuck = false; public ExtendedGallery(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ExtendedGallery(Context context, AttributeSet attrs) { super(context, attrs); } public ExtendedGallery(Context context) { super(context); } @Override public boolean onTouchEvent(MotionEvent event) { return stuck || super.onTouchEvent(event); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_DPAD_LEFT: case KeyEvent.KEYCODE_DPAD_RIGHT: return stuck || super.onKeyDown(keyCode, event); } return super.onKeyDown(keyCode, event); } public void setScrollingEnabled(boolean enabled) { stuck = !enabled; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){ return super.onFling(e1, e2, 0, velocityY); }

    }

    Now in your activity Xml in place of gallery you can put (main_acitvity) :-

    Better if put both of them inside a relative layout

            <com.pakagename.ExtendedGallery
                android:id="@+id/mygallery01"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:fadingEdge="none"
                android:spacing="0px" />
    
            <LinearLayout
                android:id="@+id/image_count"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="#00000000"
                android:orientation="horizontal"
                android:gravity="center" >
            </LinearLayout>
    
    

    Main Activity :-

    package com.feastcard;

    import android.content.Context;

    import android.content.Intent;

    import android.graphics.Bitmap;

    import android.graphics.Typeface;

    import android.net.Uri;

    import android.os.Bundle;

    import android.view.LayoutInflater;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.view.ViewGroup;

    import android.widget.AdapterView;

    import android.widget.AdapterView.OnItemSelectedListener;

    import android.widget.BaseAdapter;

    import android.widget.ImageView;

    import android.widget.LinearLayout;

    import android.widget.RelativeLayout;

    import android.widget.TextView;

    import com.pakagename.data.ImagePath; import com.pakagename.util.ImageThreadLoader;

    public class MainActivity extends CustomMainScreen implements OnClickListener{

    private MainActivity mainactivity; private ExtendedGallery galleryView; private ImageThreadLoader imageThreadLoader; private int count; private LinearLayout count_layout; static TextView page_text[]; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mainactivity= this; setContentView(R.layout.main_activity); galleryView= (ExtendedGallery ) resturantDetail.findViewById(R.id.mygallery01); ImageAdapter adapter = new ImageAdapter(resturantDetail); galleryView.setAdapter(adapter); galleryView.setScrollingEnabled(true); count_layout = (LinearLayout) findViewById(R.id.image_count); count = resturantDetailView.getAdapter().getCount(); System.out.println("Gallery Image Count======>>>" + count); 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]); } galleryView.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { // TODO Auto-generated method stub System.out.println("Item Selected Position=======>>>" + pos); for (int i = 0; i < count; i++) { MainActivity.page_text[i] .setTextColor(android.graphics.Color.GRAY); } MainActivity.page_text[pos] .setTextColor(android.graphics.Color.WHITE); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } });

    Now the Image Adapter class used for setting up the images in Custom Gallery view :

    public class ImageAdapter extends BaseAdapter {
            /** The parent context */
            //private Context myContext;
            @SuppressWarnings("unused")
            private Context mContext; 
            LayoutInflater mInflater;

    /** Simple Constructor saving the 'parent' context. */ public ImageAdapter(Context c) { mContext = c; mInflater=LayoutInflater.from(c); } public int getCount() { return imageArray.size(); // here i'm using a string array(imageArray) containing the urls of the images } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } // Returns a new ImageView to be displayed, public View getView(int position, View convertView, ViewGroup parent) { final ImagePath path = imageArray.get(position); // Get a View to display image data if(convertView==null) { convertView=mInflater.inflate(R.layout.image_slide_description, null); } System.out.println(" POSITION "+position); Bitmap cachedImage = null; final ImageView iv = (ImageView)convertView.findViewById(R.id.image); iv.setTag(path.getImagepath()); System.out.println("SSSSSSSSSS"+path.getImagepath()); try { cachedImage = imageThreadLoader.loadImage(path.getImagepath(), new ImageThreadLoader.ImageLoadedListener() { public void imageLoaded(Bitmap imageBitmap) { System.out.println("Inside Image Load of ImageAdapter"); iv.setImageBitmap(imageBitmap); iv.setScaleType(ImageView.ScaleType.FIT_XY); notifyDataSetChanged(); } }); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch(NullPointerException e) { e.printStackTrace(); } if(cachedImage!=null) { System.out.println("Inside Cached Image of ImageAdapter"); iv.setImageBitmap(cachedImage); iv.setScaleType(ImageView.ScaleType.FIT_XY); // The preferred Gallery item background } return convertView; } }

    layout inflated inside the adapter class :

    imageslidedescription :-

    <ImageView 
        android:id="@+id/image" 
        android:layout_width="250dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:layout_margin="30dp"
        android:contentDescription="@string/image_view"/>
    

    Hope this will help you. Still if above code need some modification for better or you have other some suggestions regarding this ,feel free to post.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: