Tuesday 16 July 2013

Android: View pager imageslider in android


public class MainActivity extends Activity {

public ViewPager viewPager, viewPagerThumbnails;
public Integer[] pics = { R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher };
public Gallery ga;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new ViewAdapter());

ga = (Gallery) findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));

ga.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
Toast.makeText(getBaseContext(),
"You have selected picture " + position,
Toast.LENGTH_SHORT).show();
viewPager.setCurrentItem(position);
}
});

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

@Override
public void onPageSelected(int position) {
ga.setSelection(position);

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrollStateChanged(int arg0) {

}
});

}

private class ViewAdapter extends PagerAdapter {

@Override
public int getCount() {
return 4;
}

@Override
public Object instantiateItem(View v, int position) {
View layout = getLayoutInflater().inflate(R.layout.responsive_item,
null);

((ViewPager) v).addView(layout);
return layout;
}

@Override
public void destroyItem(View v, int position, Object view) {
((ViewPager) v).removeView((View) view);
}

@Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}

}

public class ImageAdapter extends BaseAdapter {

public int imageBackground;

public ImageAdapter(MainActivity mainActivity) {
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return pics.length;
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv = new ImageView(getApplicationContext());
iv.setImageResource(pics[position]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(100, 80));
iv.setBackgroundResource(imageBackground);
return iv;
}

}

}

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="300dip"
        android:layout_gravity="center_horizontal|center_vertical" />

    <Gallery
        android:id="@+id/Gallery01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom" >
    </Gallery>

</FrameLayout>

responsive_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="700dp"
        android:layout_height="400dp"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/text1"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="21dp"
        android:text="View Port: 1024x768" />

    <ImageView
        android:id="@+id/arrow11"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/text1"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="21dp"
        android:layout_toLeftOf="@+id/imageView1"
        android:src="@drawable/ic_launcher" />

    <ImageView
        android:id="@+id/arrow22"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text122"
        android:layout_toRightOf="@+id/imageView1"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/text122"
        android:layout_width="40dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/arrow22"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="44dp"
        android:layout_marginTop="30dp"
        android:text="IPad" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="port" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="land" />
    </LinearLayout>

</RelativeLayout>

styles.xml
<declare-styleable name="Gallery1">
    <attr name="android:galleryItemBackground"/>
     </declare-styleable>