// Create a ViewPager, set adapter for it, and set it's current item to position 0
(page0.xml).
androidx.viewpager.widget.ViewPager viewPager1 = new
androidx.viewpager.widget.ViewPager(this);
viewPager1.setAdapter(new MyPagerAdapter());
viewPager1.setCurrentItem(0);
// Define a new TabLayout, set it up with the ViewPager, and add it to the
AppBarLayout which surrounds the ToolBar. The AppBarLayout thus will contain
ToolBar and TabLayout.
com.google.android.material.tabs.TabLayout tabLayout = new
com.google.android.material.tabs.TabLayout(this);
tabLayout.setTabMode(com.google.android.material.tabs.TabLayout.MODE_SCROLLABLE);
tabLayout.setupWithViewPager(viewPager1);
((com.google.android.material.appbar.AppBarLayout)_toolbar.getParent()).addView(tab
Layout);
// Add the ViewPager to linear1 of main.xml
linear1.addView(viewPager1);