Dynamically add and remove tabs in TabLayout(material design) android

Remove tab from TabLayout … public void removeTab(int position) { if (mTabLayout.getTabCount() >= 1 && position<mTabLayout.getTabCount()) { mTabLayout.removeTabAt(position); mPagerAdapter.removeTabPage(position); } } … Add a removeTabPage method to your PagerAdapter … public void removeTabPage(int position) { if (!tabItems.isEmpty() && position<tabItems.size()) { tabItems.remove(position); notifyDataSetChanged(); } } … Add a Tab … private void addTab(String title) { mTabLayout.addTab(mTabLayout.newTab().setText(title)); … Read more

Android Tab layout: Wrap tab indicator width with respect to tab title

Yes, it’s possible to wrap tab indicator as title setting padding to 0 public void wrapTabIndicatorToTitle(TabLayout tabLayout, int externalMargin, int internalMargin) { View tabStrip = tabLayout.getChildAt(0); if (tabStrip instanceof ViewGroup) { ViewGroup tabStripGroup = (ViewGroup) tabStrip; int childCount = ((ViewGroup) tabStrip).getChildCount(); for (int i = 0; i < childCount; i++) { View tabView = tabStripGroup.getChildAt(i); … Read more

Tab change listener android

You can use OnTabChangeListener.See below TabLayout tabLayout = new TabLayout(this); tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { //do stuff here } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); hope it help.

TabLayout set spacing or margin each tab

Been fighting this problem for a while too, found the solution on this thread : Android Design Support Library TabLayout using custom tabs layout but layout wrapping the tabs <!– Add the padding to tabPaddingStart and/or tabPaddingEnd –> <android.support.design.widget.TabLayout android:id=”@+id/tabLayout” android:layout_width=”match_parent” android:layout_height=”@dimen/tab_layout_height” app:tabPaddingStart=”10dp” app:tabPaddingEnd=”10dp”>

Disable TabLayout

I had the same problem and I solved it disabling touch event on tabs with the following code: LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0)); for(int i = 0; i < tabStrip.getChildCount(); i++) { tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return true; } }); }

TabLayout not filling width when tabMode set to ‘scrollable’

androidx version: <androidx.viewpager.widget.ViewPager android:id=”@+id/view_pager” android:layout_width=”match_parent” android:layout_height=”match_parent” app:layout_behavior=”@string/appbar_scrolling_view_behavior” > <com.google.android.material.tabs.TabLayout android:id=”@+id/tabs” android:layout_width=”match_parent” android:layout_height=”wrap_content” app:tabMaxWidth=”0dp” app:tabGravity=”fill” app:tabMode=”fixed” /> </androidx.viewpager.widget.ViewPager> ***** BELOW IS OLD ANSWER ***** Try this one, it’s a workaround which sets tabMaxWidth=”0dp”, tabGravity=”fill” and tabMode=”fixed”. <android.support.v4.view.ViewPager android:id=”@+id/container” android:layout_width=”match_parent” android:layout_height=”match_parent” app:layout_behavior=”@string/appbar_scrolling_view_behavior”> <android.support.design.widget.TabLayout android:id=”@+id/tabs” android:layout_width=”match_parent” android:layout_height=”wrap_content” app:tabMaxWidth=”0dp” app:tabGravity=”fill” app:tabMode=”fixed”/> </android.support.v4.view.ViewPager> Screenshot on a 10 inch tablet:

How to change selected Tab Text color using TabLayout from code in Android?

It’s so simple using XML. Just add the following 2 attributes in your tab layout. app:tabSelectedTextColor=”@color/color_primary_text” app:tabTextColor=”@color/color_secondary_text” So, your code would look something like this. <android.support.design.widget.TabLayout android:id=”@+id/tab_layout” android:layout_width=”match_parent” android:layout_height=”wrap_content” android:layout_alignParentBottom=”true” android:layout_alignParentStart=”true” android:layout_gravity=”bottom” android:background=”@color/button_background” android:fillViewport=”true” app:tabBackground=”@drawable/fixed_bottom_button” app:tabIndicatorColor=”@color/color_primary_text” app:tabMode=”fixed” app:tabSelectedTextColor=”@color/color_primary_text” app:tabTextColor=”@color/color_secondary_text” />

TabLayout without using ViewPager

I found addOnTabSelectedListener: tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { if(tabLayout.getSelectedTabPosition() == 0){ Toast.makeText(MainActivity.this, “Tab ” + tabLayout.getSelectedTabPosition(), Toast.LENGTH_LONG).show(); }else if(tabLayout.getSelectedTabPosition() == 1){ Toast.makeText(MainActivity.this, “Tab ” + tabLayout.getSelectedTabPosition(), Toast.LENGTH_LONG).show(); }else if(tabLayout.getSelectedTabPosition() == 2){ Toast.makeText(MainActivity.this, “Tab ” + tabLayout.getSelectedTabPosition(), Toast.LENGTH_LONG).show(); }else if(tabLayout.getSelectedTabPosition() == 3){ Toast.makeText(MainActivity.this, “Tab ” + tabLayout.getSelectedTabPosition(), Toast.LENGTH_LONG).show(); }else if(tabLayout.getSelectedTabPosition() == 4){ … Read more

TabLayout tabs text not displaying

The problem is you’re calling setupWithViewPager() after setting up your tabs with the addTab() calls, effectively overwriting them. From the documentation of TabLayout regarding setupWithViewPager(): The tabs displayed in this layout will be populated from the ViewPager adapter’s page titles. If you would like to use your TabLayout with a ViewPager, you should override getPageTitle() … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)