In this tutorial we will implement ViewPager with ImageView in android.
try this code
stepe 1.create ViewPagerAdapter.java class in src folder..
...................................................................................................................................................................
package com.androidbeginner.testcode;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ViewPagerAdapter extends PagerAdapter {
Context context;
String[] number;
String[] name;
String[] weight;
int[] ball;
LayoutInflater inflater;
public ViewPagerAdapter(Context context, String[] number, String[] name,
String[] weight, int[] ball) {
this.context = context;
this.number = number;
this.name = name;
this.weight = weight;
this.ball = ball;
}
@Override
public int getCount() {
return number.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// Declare Variables
TextView nubmer1;
TextView name1;
TextView weight1;
ImageView img1;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.viewpager_item, container,
false);
nubmer1 = (TextView) itemView.findViewById(R.id.number1);
name1 = (TextView) itemView.findViewById(R.id.ball);
weight1 = (TextView) itemView.findViewById(R.id.weight1);
// Capture position and set to the TextViews
nubmer1.setText(number[position]);
name1.setText(name[position]);
weight1.setText(weight[position]);
// Locate the ImageView in viewpager_item.xml
img1 = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set to the ImageView
img1.setImageResource(ball[position]);
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);
return itemView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);
}
}
...................................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp" >
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number:-" />
<TextView
android:id="@+id/number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/number" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/number"
android:text="Name:-" />
<TextView
android:id="@+id/ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/number1"
android:layout_toRightOf="@+id/name" />
<TextView
android:id="@+id/weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:text="Weight:-" />
<TextView
android:id="@+id/weight1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ball"
android:layout_toRightOf="@+id/weight" />
<ImageView
android:id="@+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000000"
android:padding="1dp" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp" >
<TextView
android:id="@+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number:-" />
<TextView
android:id="@+id/number1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/number" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/number"
android:text="Name:-" />
<TextView
android:id="@+id/ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/number1"
android:layout_toRightOf="@+id/name" />
<TextView
android:id="@+id/weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:text="Weight:-" />
<TextView
android:id="@+id/weight1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ball"
android:layout_toRightOf="@+id/weight" />
<ImageView
android:id="@+id/flag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#000000"
android:padding="1dp" />
</RelativeLayout>
stepe 3. create MainActivity.java inside src folder..
...................................................................................................................................................................
package com.androidbeginner.testcode;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
public class MainActivity extends Activity {
ViewPager viewPager;
PagerAdapter adapter;
String[] number;
String[] name;
String[] weight;
int[] ballimg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
number = new String[] { "1", "2", "3", "4", "5" };
name = new String[] { "football", "football", "football","football" };
weight = new String[] { "70gm", "150gm","315gm", "90gm", "193gm"};
ballimg = new int[] { R.drawable.a, R.drawable.b,
R.drawable.c, R.drawable.d,
R.drawable.e };
viewPager = (ViewPager) findViewById(R.id.myviewpager);
adapter = new ViewPagerAdapter(MainActivity.this, number, name, weight, ballimg);
viewPager.setAdapter(adapter);
}
}
stepe4. 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>
0 comments