• Register
0 votes

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

Please log in or register to answer this question.

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()) {

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

public void addTabPage(String title) {

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

5 2 1
4,980 points

Related questions

0 votes
1 answer 9 views
Problem: error: program type already present: android.support.design.widget.coordinatorlayout$behavior
asked Feb 19 Lucky Guy 1.4k points
0 votes
2 answers 60 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
1 answer 5 views
Problem: could not resolve com.android.support:appcompat-v7:26.1.0.
asked 5 days ago Dan phillip 1.9k points
0 votes
2 answers 70 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, 2020 alecxe 7.5k points
1 vote
1 answer 257 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 966 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, 2020 jwilliam 3.9k points
0 votes
1 answer 7 views
Problem: error: program type already present: android.support.v4.app.fragmenttransitioncompat21$1
asked Feb 20 Lucky Guy 1.4k points
0 votes
1 answer 17 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, 2020 Daniel Anderson 4k points
0 votes
1 answer 1.7K views
Problem : I am learning Android Studio. I am trying to add implemntation 'com.google.android.material:material:1.0.0-alpha1' but if I try to build the project then my Android Studio gives below error: Program type already present: android.support.v4.app. ... .support.v4.app.INotificationSideChannel$Stub$Proxy, sources=[Unknown source file], tool name=Optional.of(D8)} How can I fix the above error?
asked Feb 19, 2020 mphil 2.3k points
0 votes
1 answer 37 views