How to RE-SELECT tab in TabLayout programmatically?
Hii developer in this Android tutorial I am sharing how to re-select Tab when I move to any other activity form TabLayout Tab. What an easy way to know which tab you can go to any other activity and come back Tablaout activity and select the same Tab. How to RE-SELECT tab in TabLayout programmatically?
In this AndroidX TabLayout example, I also share shown to the used tab bar in android. With a simple way and control over tab navigation. How to RE-SELECT tab in TabLayout programmatically?
Step 1: TabActivity Layout file code add these in your XML file.
<?xml version="1.0" encoding="utf-8"?> <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context=".TabScreen"> <com.google.android.material.appbar.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> <com.google.android.material.tabs.TabLayout android:id="@+id/Home_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabSelectedTextColor="@color/BlueDark" app:tabTextColor="@color/Black" app:tabMode="fixed" app:tabGravity="fill"/> </com.google.android.material.appbar.AppBarLayout> <androidx.viewpager.widget.ViewPager android:id="@+id/Home_Page_View" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout>
Step 2: Create Tab-bar Adapter Class Pager.java Class.
In your Adapter class, you can control the tab and set the title on a tab. And Extends FragmentStatePagerAdapter in your class.
public class Pager extends FragmentStatePagerAdapter { int tabcount; Context ctx; private String [] Titles = {"PageTitel ", "PageTitel",}; public Pager(FragmentManager fm, int tabcount, Context ctx) { super(fm); this.tabcount = tabcount; this.ctx = ctx; } @Override public int getCount() { return tabcount; } @Override public CharSequence getPageTitle(int position) { return Titles[position]; } @Override public Fragment getItem(int position) { switch (position) { case 0: tab1 tab1 = new tab1(); return tab1; case 1: tab2 tab2 = new Tab2(); return tab2; default: return null; } } }
Step 3: Main Java Class Implementation.
public class Event_TabScreen extends AppCompatActivity implements TabLayout.OnTabSelectedListener { private TabLayout tabLayout; private ViewPager viewPager; public static Context ctx; Pager adapter; public static int expired, paid; Boolean isConnection; public String Seclettab="" ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_event__tab_screen); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); Seclettab=getIntent().getStringExtra("Tab"); tabLayout = (TabLayout) findViewById(R.id.Home_tabs); viewPager = (ViewPager) findViewById(R.id.Home_Page_View); tabLayout.addTab(tabLayout.newTab().setText("OageTitle")); tabLayout.addTab(tabLayout.newTab().setText("PageTitle")); tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); tabLayout.setupWithViewPager(viewPager); adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount(), ctx); viewPager.setAdapter(adapter); if(Seclettab.equals(null)||equals("")||equals("null")) { viewPager.setCurrentItem(0); } else if(Seclettab.equals("Tab2")) { viewPager.setCurrentItem(1); }else { viewPager.setCurrentItem(0); } viewPager.setOffscreenPageLimit(1); tabLayout.addOnTabSelectedListener(this); ViewGroup vg = (ViewGroup) tabLayout.getChildAt(0); int tabsCount = vg.getChildCount(); for (int j = 0; j < tabsCount; j++) { ViewGroup vgTab = (ViewGroup) vg.getChildAt(j); int tabChildsCount = vgTab.getChildCount(); for (int i = 0; i < tabChildsCount; i++) { View tabViewChild = vgTab.getChildAt(i); if (tabViewChild instanceof TextView) { ((TextView) tabViewChild).setTypeface(Typeface.DEFAULT, Typeface.BOLD); } } } } @Override public void onTabSelected(TabLayout.Tab tab) { viewPager.setCurrentItem(tab.getPosition()); } @Override public void onTabReselected(TabLayout.Tab tab) { } @Override public void onTabUnselected(TabLayout.Tab tab) { }