Wednesday, 20 June 2018

How to Generate Dynamic Tab in Android

- First create MainActivity.class and activity_main.xml

- Add following code in MainActivity.java class

  public class MainActivity extends FragmentActivity {

    ViewPager viewPager;
    TabLayout tabLayout;
    TabsPagerAdapter tabsPagerAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    //Reference of Components
        tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        viewPager = (ViewPager) findViewById(R.id.pager);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    setTabData();
     }
  }

- Add following code in activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          android:weightSum="1">

             <android.support.design.widget.TabLayout
                android:id="@+id/tab_layout"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="0.08"
                android:background="@color/btn_login"
                android:elevation="6dp"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:tabIndicatorColor="@android:color/white"
                app:tabMode="fixed" />

             <android.support.v4.view.ViewPager
                android:id="@+id/pager"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_weight="0.92" />
     </LinearLayout>
</LinearLayout>

- To Add this method in your Class and call this method in your class

    public void setTabData() {
        String[] str_tab_name = {"Test", "Test 1","Test 2"};
        //Set Tab Data
        ArrayList<Fragment> list = new ArrayList<Fragment>();
        tabLayout.removeAllTabs();
        for (int i = 0; i < str_tab_name.length; i++) {
            tabLayout.addTab(tabLayout.newTab().setText("" + str_tab_name[i]));
            TestFragment testFragment = new TestFragment();
            list.add(testFragment);
            Bundle bundle = new Bundle();
            bundle.putInt("tab_position", i);
            testFragment.setArguments(bundle);
        
        }

        //viewPager.setOffscreenPageLimit(1);
        tabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager(), list);
        viewPager.setAdapter(tabsPagerAdapter);
        viewPager.setOffscreenPageLimit(1);
        viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {

            }
        });

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
     
    }

- To Create Fragment Class For Example "TestFragment" and create xml file according it is fragment_test

   public class TestFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_test, container, false);
    }
  }


- Add Following code in fragment_test.xml file

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

  </LinearLayout>

No comments:

Post a Comment