28

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);
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
vinothp
  • 9,939
  • 19
  • 61
  • 103

7 Answers7

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.

Kennedy Nyaga
  • 3,455
  • 1
  • 26
  • 25
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.

vinothp
  • 9,939
  • 19
  • 61
  • 103
  • 1
    `android.R.id.tabhost` will throw a nullpointerexception. You are already calling `getActivity.findViewById` `findViewById(R.id.tabhost)` is the right option – Ojonugwa Jude Ochalifu Dec 13 '16 at 22:51
19

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();
BENN1TH
  • 2,003
  • 2
  • 31
  • 42
  • 2
    this is working answer... upper ones not working because tablayout is null in fragments even if we pass it via constructor.. – Mehtab Jul 02 '18 at 17:51
7

Take a look at this answer: https://stackoverflow.com/a/5460651/198996

((TabActivity) getParent()).getTabHost().setCurrentTab(2)
Community
  • 1
  • 1
TomTasche
  • 5,448
  • 7
  • 41
  • 67
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)

lea.cotan
  • 203
  • 2
  • 10
1

for kotlin , Please use this form fragment

 activity!!.<tab_id_in_xml>.getTabAt(tab_number)!!.select()
Abhay Pratap
  • 1,886
  • 11
  • 15
0

Here's another approach in 'newer' kotlin:

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

using ?. instead of !! to avoid asserting.

Roar Grønmo
  • 2,926
  • 2
  • 24
  • 37