How to switch tabs programmatically in Android from fragment?
Asked Answered
P

7

29

I have implemented a TabActivity which extends FragmentActivity. It has 5 tabs each tab is a Fragment. What i am looking for is to switch between the tabs programmatically.

For eg: if i am in tab4. On button click I want to move from tab4 to tab1. Tried a lot but couldn't find the solution for this.

Tried with the following but it doesn't help.

From SecondTab

public void switchTabInActivity(String value){
    FirstTab parent;
    parent = (FirstTab) getActivity().getParent();
    parent.switchTab(value);
}

TabActivity

  /** To Change Tab*/
public void switchTab(String tabno){ 
    this.onTabChanged(tabno);
}
Porscheporsena answered 1/5, 2013 at 8:51 Comment(0)
P
21

Finally i can switch between the tabs programatically from Fragments using the following line of code

  TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
  host.setCurrentTab(2);

Hope it will help some one.

Porscheporsena answered 1/5, 2013 at 9:13 Comment(1)
android.R.id.tabhost will throw a nullpointerexception. You are already calling getActivity.findViewById findViewById(R.id.tabhost) is the right optionHoagland
U
31

for Material support you switch the tablayout from a fragment in the following ways:

1) send a broadcast that is received by the parent activity which then modifies the tab.

context.sendBroadcast(yourintent);

2.) A modification of vino's answer,

TabLayout tabhost = (TabLayout) getActivity().findViewById(R.id.tabLayout);
tabhost.getTabAt(2).select();

tablayout is the id of the tablayout as defined in your main xml.

Uhl answered 24/8, 2015 at 19:25 Comment(5)
Yes it works, just for information tab indexes starts from 0.Murex
Hi @Kennedy, my tabhost is not getting initialised. It is showing null. What could be the mistake for NullPointerException??Verminous
It's working now, error was due to a silly mistake of mine. :)Verminous
Hi! Can someone please help me? I am getting an error: Cannot resolve method 'getActivity()'.Oldtime
@LloydDominic make sure you are working from a fragment.Uhl
P
21

Finally i can switch between the tabs programatically from Fragments using the following line of code

  TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
  host.setCurrentTab(2);

Hope it will help some one.

Porscheporsena answered 1/5, 2013 at 9:13 Comment(1)
android.R.id.tabhost will throw a nullpointerexception. You are already calling getActivity.findViewById findViewById(R.id.tabhost) is the right optionHoagland
I
20

I have tabs (using TabLayout not TabHost(depreciated))(with Fragments) in my Main Activity in which in my first tab(fragment) with a click listener in the fragment which is for changing the current tab in my MainActivity.

I successfully change the current tab via the below in the onCreateView() method within the fragment.

TabLayout tabs = (TabLayout)((MainActivity)getActivity()).findViewById(R.id.tabs);
tabs.getTabAt(1).select();
Imprisonment answered 28/4, 2018 at 3:45 Comment(2)
this is working answer... upper ones not working because tablayout is null in fragments even if we pass it via constructor..Esperanzaespial
Or a little more succinctly: TabLayout tabs = requireActivity().findViewById(R.id.tabs); tabs.getTabAt(1).select();Ribaldry
L
7

Take a look at this answer: https://mcmap.net/q/501311/-how-to-change-tab-of-a-tabactivity-from-an-activity-started-by-the-tabactivity-or-change-current-tab

((TabActivity) getParent()).getTabHost().setCurrentTab(2)
Lolalolande answered 1/5, 2013 at 8:58 Comment(8)
Thanks Tom i think thats from activity i am looking from FragmentPorscheporsena
@Vino That's from the Fragment. The getParent() is a method in Fragment which returns the Activity.Clank
@Vino try getActivity() instead of getParent() then.Lolalolande
yes tried that but i am facing The method getTabHost() is undefined for the type TabActivity ((TabActivity) getActivity()).getTabHost().setCurrentTab(3);Porscheporsena
use switchTab() and cast it as your concrete implementation rather than TabActivity then. Note that TabActivity is deprecated.Clank
@Vino Implement a method getTabHost() in your TabActivity, which returns the TabHost...Lolalolande
Found an answer Tom.I have answered my question. Thanks for your support.Porscheporsena
Well, that's basically what I said. Anyway, good to hear you solved your problem.Lolalolande
B
5

If you're using TabLayout instead of TabHost , I suggest a modification to BENN1TH's answer that worked for me:

  TabLayout tabs = getActivity().findViewById(R.id.tab_layout);
  tabs.getTabAt(tabNumber).select(); 

(The difference is R.id.tab_layout)

Bowdlerize answered 25/4, 2019 at 9:19 Comment(0)
K
1

for kotlin , Please use this form fragment

 activity!!.<tab_id_in_xml>.getTabAt(tab_number)!!.select()
Krawczyk answered 10/5, 2021 at 13:53 Comment(0)
C
0

Here's another approach in 'newer' kotlin:

requireActivity().findViewById<TabLayout>(R.id.tab_layout)?.getTabAt(destinationTabNumber)?.select()

using ?. instead of !! to avoid asserting.

Carreno answered 29/1, 2023 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.