Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to make Custom ViewPager in android

    • 0
    • 0
    • 0
    • 0
    • 2
    • 0
    • 0
    • 0
    • 3.28k
    Comment on it

    In the below example I have created custom ViewPager in android. Here first I have added TextView and ViewPager in activity_main.xml and In MainActivity I have used MyPagerAdapter class and extends with the PagerAdapter class , In resource i have added five images pages and I have also used Toast function to get page position when you clicked on image. You can see below program it will clearly describe you How to make Custom ViewPager in android.

    Srep(1)activity_main.xml layout-
     

    <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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        tools:context="com.example.androidviewpagerapp.MainActivity" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:autoLink="web"
            android:textSize="20dp"
            android:textColor="#eeec3e24"
            android:text="Custom ViewPager"
            android:textStyle="bold" />
    
        <android.support.v4.view.ViewPager
            android:id="@+id/myviewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </LinearLayout>
    
    

    Srep(2)MainActivity-
     

    public class MainActivity extends Activity {
    
        ViewPager viewPager;
        MyPagerAdapter myPagerAdapter;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            viewPager = (ViewPager)findViewById(R.id.myviewpager);
            myPagerAdapter = new MyPagerAdapter();
            viewPager.setAdapter(myPagerAdapter);
    
        }
    
        private class MyPagerAdapter extends PagerAdapter {
    
            int NumberOfPages = 5;
    
            int[] res = {
                   R.drawable.one,
                   R.drawable.  two,
                    R.drawable.three,
                    R.drawable.four,
                    R.drawable.one};
            int[] backgroundcolor = {
                    0xFF101010,
                    0xFF202020,
                    0xFF303030,
                    0xFF404040,
                    0xFF505050};
    
            @Override
            public int getCount() {
                return NumberOfPages;
            }
    
            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
    
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
    
    
                TextView textView = new TextView(MainActivity.this);
                textView.setTextColor(Color.WHITE);
                textView.setTextSize(30);
                textView.setTypeface(Typeface.DEFAULT_BOLD);
                textView.setText(String.valueOf(position));
    
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageResource(res[position]);
                LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                imageView.setLayoutParams(imageParams);
    
                LinearLayout layout = new LinearLayout(MainActivity.this);
                layout.setOrientation(LinearLayout.VERTICAL);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                layout.setBackgroundColor(backgroundcolor[position]);
                layout.setLayoutParams(layoutParams);
                layout.addView(textView);
                layout.addView(imageView);
    
                final int page = position;
                layout.setOnClickListener(new View.OnClickListener(){
    
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MainActivity.this,
                                "Page " + page + " clicked",
                                Toast.LENGTH_SHORT).show();
                    }});
    
                container.addView(layout);
                return layout;
            }
    
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView((LinearLayout)object);
            }
    
        }
    
    }

     

 2 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: