How to dynamically add and remove view from ViewPager in Android

In this android programming source code example, we are going to dynamically add and remove view from ViewPager in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to dynamically add and remove view from ViewPager in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ededed">

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</RelativeLayout>

MainActivity.java

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.bluapp.androidview.R;

import java.util.ArrayList;
import java.util.List;

public class TabLayoutAndViewPagerActivity14 extends AppCompatActivity {
    private ViewPager viewPager;
    private MyAdapter myAdapter;

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

        LayoutInflater inflater = getLayoutInflater();
        RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.tabwithviewpager14,null);
        myAdapter.addView(relativeLayout, 0);
        myAdapter.notifyDataSetChanged();

    }

    public void addView(View newPage){
        int pageindex = myAdapter.addView(newPage);
        viewPager.setCurrentItem(pageindex, true);
    }

    public void removeView(View defunctPage){
        int pageindex = myAdapter.removeView(viewPager, defunctPage);
        if(pageindex == myAdapter.getCount())
            pageindex--;
        viewPager.setCurrentItem(pageindex);
    }

    public View getCurrentPage(){
        return  myAdapter.getView(viewPager.getCurrentItem());
    }

    public void setCurrentPage(View pageToShow){
        viewPager.setCurrentItem(myAdapter.getItemPosition(pageToShow), true);
    }

    private class MyAdapter extends PagerAdapter {
        private final List<View> views = new ArrayList<View>();


        @Override
        public int getItemPosition(Object object) {
            int index = views.indexOf(object);
            if(index == -1)
                return POSITION_NONE;
            else
            return index;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(views.get(position));
        }

        @Override
        public int getCount() {
            return views.size();
        }

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

        public int addView(View view){
            return addView(view, views.size());
        }


        public int addView(View view, int position){
            views.add(position, view);
           return position;
        }

        public int removeView(ViewPager pager, View view){
            return removeView(pager, views.indexOf(view));
        }

        public int removeView(ViewPager pager, int position){
            pager.setAdapter(null);
            views.remove(position);
            pager.setAdapter(this);
            return position;
        }

        public View getView(int position){
            return views.get(position);
        }

    }

}

tabwithviewpager14.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:background="#ededed">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Dynamically Add View and Remove View"
        android:gravity="center"
        android:textSize="20sp"/>

</RelativeLayout>

 

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.