How to switch tabs programmatically in Android from fragment? - android

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);
}

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.

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.

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

Take a look at this answer: https://stackoverflow.com/a/5460651/198996
((TabActivity) getParent()).getTabHost().setCurrentTab(2)

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)

for kotlin , Please use this form fragment
activity!!.<tab_id_in_xml>.getTabAt(tab_number)!!.select()

Here's another approach in 'newer' kotlin:
requireActivity().findViewById<TabLayout>(R.id.tab_layout)?.getTabAt(destinationTabNumber)?.select()
using ?. instead of !! to avoid asserting.

Related

MvvmCross ViewPager not show in Activity

I want to show a few tabs sharing the same viewmodel with viewpager in an activity. I am using MvvmCross 5.6.2.
When I do that in a fragment, the viewpager is shown properly. Using the same method, I add tab fragments to the viewpager in an activity with code like this:
var viewPagerFragmentList = new List<MvxViewPagerFragmentInfo>
{
new MvxViewPagerFragmentInfo("Personal", typeof (SettingPersonalFragment), ViewModel),
new MvxViewPagerFragmentInfo("Preference", typeof (SettingPreferenceFragment), ViewModel)
};
viewPager.Adapter = new MvxCachingFragmentStatePagerAdapter(this, SupportFragmentManager, viewPagerFragmentList);
var tabLayout = FindViewById<TabLayout>(Resource.Id.tabs_fragment_setting_viewpager);
tabLayout.SetupWithViewPager(viewPager);
The tab fragments are not shown on screen, though the tab fragment's OnCreate and OnCreateView are executed.
I found the latest sample Playground is showing tabs in activity too. Following the sample, I register the fragment:
[MvxTabLayoutPresentation(TabLayoutResourceId = Resource.Id.tabs_fragment_setting_viewpager, ViewPagerResourceId = Resource.Id.viewpager_fragment_setting_viewpager, Title = "Personal", ActivityHostViewModelType = typeof(SettingViewModel))]
No luck with that. The last thing I found in Playground to show a tab is to navigate to the fragment viewmodel:
_navigationService.Navigate<Tab2ViewModel>();
_navigationService.Navigate<Tab3ViewModel>();
However, in my case, I do not have a viewmodel for each fragment. The workaround on my hand is to move the viewpager to a fragment in the activity.
How could I show the viewpager fragments without their own viewmodels in an activity?
Thanks,
Nick
The above code that works in fragment also work in activity. There is an issue in the xml causing the viewpager not showing. Sorry.
Thanks everyone who helped.

How to set a specific tab when opening a Sliding Tab Layout activity?

I tried this tutorial and it works great, but now I would like to know if it is possible, when opening the application, to go directly to a specific tab and not the first one (at left) by default ?
Ex : I would like the application to open directly on "events" tab :
I searched into the code, but didn't find anything relevant.
Thanks folks
EDIT :
Here is the way :
Just add pager.setCurrentItem(1); in your MainActivity (the tabs container).
ViewPager is most often used in conjunction with Fragment, which is a
convenient way to supply and manage the lifecycle of each page.
You can use setCurrentItem // Set the currently selected page.
Set the currently selected page. If the ViewPager has already been
through its first layout with its current adapter there will be a
smooth animated transition between the current item and the specified
item.
Finally,
Your_View_Pager_Obj.setCurrentItem(1); // call into onCreate(Bundle savedInstanceState)
Use the following code along with the viewPager and tablayout
pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
you can use setCurrentTab(index) in onResume() of MainActivity where index is the index of tab you want to go to.
Add to the MainActivity.java
private static final int EVENTS = 1;
private ViewPager mViewPager;
mViewPager = (ViewPager) findViewById(R.id.pager);
#Override
public void onStart() {
super.onStart();
mViewPager.setCurrentItem(EVENTS);
}

Switch ActionBar Tabs from within Fragment, Android

I am trying to switch between tabs (ActionBarActivity) from within a fragment that is attached to one of the tabs.
I found this solution:
TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
host.setCurrentTab(2);
but the app crashes when executed, with a NullPointerException at the second line. If I try and get any information about host, it all comes back null.
I don't have much Android experience, and so am unsure how to properly get a reference to the parent activity that holds the fragment.
EDIT
I'm not sure if this makes a difference, but I've moved the default SectionsPagerAdapter to a class of its own.
getActivity().getTabHost().setCurrentTab(2)

How to swipe a frgaments inside viewpager using robolectric in android

I want to swipe the fragments left and right which is added inside a viewpager .But i am not able to get a method in robolectric .Is there any way to achieve this in robolectric
You could do it like this..
// ... initialise your activity using Robolectric.buildActivity().create().start().visible() etc.
ViewPager viewPager = (ViewPager) myActivity.getWindow().findViewById(R.id.view_pager_tabs);
viewPager.setCurrentItem(idOfFragmentInterestedIn); // or use getCurrentItem()+1 to go right
When I tried this, I had some problems with the Fragments not getting loaded in the ViewPager when testing, but while it definitely occurred only with Robolectric, it may have had more to do with our own implementation rather than the framework, so I can't make any good suggestions out of that.
The first thing you need to do is to find a reference to your viewPager and set the page (Fragment) you want to display (As Alex Florescu said before):
ViewPager viewPager = (ViewPager) activity.getWindow().findViewById(R.id.viewPager);
viewPager.setCurrentItem(desired_tab_position);
Then you need to make sure the fragment is added to the fragment manager by doing it manually:
Fragment desiredFragment =
activity.getSupportFragmentManager().findFragmentById( R.id.fragmentId );
FragmentManager fragmentManager = activity.getSupportFragmentManager();
fragmentManager.beginTransaction().add( desiredFragment, null ).commit();
activity.getSupportFragmentManager().executePendingTransactions();
This will ensure that your fragment is actually created and you can access any view that belongs to your fragment.

switch tabs in fragmentactivity

I know it sounds stupid but I recently started studying fragments/fragmentactivity to replace tabactivity.. but the simple problem is that I don't know how to switch the tab from current tab i.e. in tabactivity hosting activities, used this to open tab 1
TabActivity tabMap = (TabActivity) getParent();
tabMap.getTabHost().setCurrentTab(1);
How can I do the same with FragmentActivity hosting tabs (fragments)??
Thanks
As long as I understood, you want something like this (assuming getParent() returns Activity)
TabHost host = getParent().findViewById(android.R.id.tabhost);
host.setCurrentTab(1);
BTW, switching to ActionBar tabs is recommended, not hard and it will look much better and you would'nt have to use deprecated stuff.

Categories

Resources