Open top menu


             Android- Display gallery using ViewPager 

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

In this Tutorila we will explain how to display gallery using viewpager in android.
try this code......


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

<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"
    tools:context="com.androidbeginner.testcode.MainActivity" >

   
   
    <android.support.v4.view.ViewPager
        android:id="@+id/myviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="2dp"/>      
   
</LinearLayout>


Stepe 2. create MainActivity.java inside src folder...
...................................................................................................................................................................

package com.androidbeginner.testcode;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MainActivity extends Activity {


ViewPager viewPager;
    int[] imageArray;
    Bundle bundle;
    final TypedArray imageArrayIcon=null;
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);    
        viewPager = (ViewPager) findViewById(R.id.myviewpager);
   ImagePagerAdapter adapter = new ImagePagerAdapter();
   viewPager.setAdapter(adapter);


}
public class ImagePagerAdapter extends PagerAdapter {

   

   private int[] mImages = new int[]{R.drawable.logoweb,R.drawable.k,R.drawable.l,R.drawable.p,R.drawable.t};
   @Override

   public int getCount() {

       return mImages.length;

   }

   @Override

   public boolean isViewFromObject(View view, Object object) {

       return view == ((ImageView) object);

   }
   @Override

   public Object instantiateItem(ViewGroup container, int position) {

       Context context = MainActivity.this;
       ImageView imageView = new ImageView(context);      
       imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
       imageView.setImageResource(mImages[position]);      
       ((ViewPager) container).addView(imageView, 0);

       return imageView;
   }
 
   @Override

   public void destroyItem(ViewGroup container, int position, Object object) {

       ((ViewPager) container).removeView((ImageView) object);

   }

}
}




thank you for watching...

0 comments