• Register
0 votes
44 views

Problem :

I am trying to dynamically add and remove the tab in my tablayout. I have already used the FragmentPagerAdapter. I have my TabLayout and inside the TabLayout I have the ViewPager.I am able to add my tabs dynamically but while trying to remove my tab the viewpagers last item is also deleted and tab is also deleted. As a result my tab is unable to show the specific fragment.

How can I fix this issue?

8 4 2
2,300 points

1 Answer

0 votes

Solution :

I hope you know that the support libraries have changed. Now with the new support library (currently using 23.2.1) you must only add to your Viewpager and you must not to the TabLayout otherwise you will end up with the new behavior showing double tabs and other very funky and different behavior.

I am giving you the below methods which you can use in your code to fix your issues.

You need to add the removeTabPage method as shown below to your PagerAdapter

public void removeTabPage(int position) {
    if (!tabItems.isEmpty() && position<tabItems.size()) {
          tabItems.remove(position);
          notifyDataSetChanged();
    }
}

Also you need to add the addTabPage method as shown below  to your PagerAdapter

public void addTabPage(String title) {
      tabItems.add(title);
      notifyDataSetChanged();
}

If you want to remove all the tabs from the tabLayout then you can use below code

tabLayout.removeAllTabs();
5 2 1
4,980 points

Related questions

0 votes
2 answers 50 views
50 views
Problem : I am facing the following issue while building my project. I haven't used any CoordinatorLayout in my project. I have just added it as a dependency in my build.gradle : I am currently using Android Studio 3.2 Canary 4. ... ..... /windows/Other/app/build/intermediates/transforms/dexBuilder/debug/304.jar Program type already present: android.support.design.widget.CoordinatorLayout$Behavior
asked Dec 5, 2019 alecxe 7.5k points
0 votes
2 answers 59 views
59 views
Problem : After updating my android studio to android studio 2.3 I facing below error message. I know it is giving me just the hint as my app is running normally but it looks really strange. &ldquo;All com.android.support libraries must use the exact same version ... 1, 24.0.0. Examples include com.android.support:animated-vector-drawable:25.1.1 and com.android.support:mediarouter-v7:24.0.0&rdquo;
asked Jan 9 alecxe 7.5k points
1 vote
1 answer 218 views
218 views
Problem : After changing compileSdkVersion 23 and targetSdkVersion 23 I am getting an error error: resource android:style/textappearance.material.widget.button.borderless.colored not found.
asked Nov 11, 2019 peterlaw 6.9k points
0 votes
2 answers 630 views
630 views
Problem : I want to upload the finished android app to a android market but I am getting below error: &ldquo;You need to use a different package name because "com.example" is restricted.&rdquo;
asked Jan 22 jwilliam 3.9k points
0 votes
1 answer 13 views
13 views
I have a mobile friendly blog. There is different layout i need to set it according android layout. That will fit in all screen sizes? Can any one here show me the path how to do it?
asked Sep 25 Daniel Anderson 4k points