I have a Tabhost in my app with 3 tabs. The tabs are all working fine.
Now I want to perform some additional logic when the tab is selected?.
For Example: In one of my tabs, I provide an option for the user to sort things in different order and update the another tab.
how can we get the click event of TabHost?
I have updated the Tab Creation (Activity) part.
Thanks in Advance.
[Activity(Label = "My Activity")]
public class TabSearch : TabActivity
{
protected override void OnCreate(Bundle bundle)
{
try
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Search_WOTab);
/* ******************** Adding 2 Tab Controls and setting Activity classes to Tabs added ******************** */
TabHost.TabSpec tspec;
Intent intent;
intent = new Intent(this, typeof(WOSearch));
intent.AddFlags(ActivityFlags.NewTask);
tspec = TabHost.NewTabSpec("Search");
tspec.SetIndicator("Search", Resources.GetDrawable(Resource.Drawable.Search));
tspec.SetContent(intent);
TabHost.AddTab(tspec);
intent = new Intent(this, typeof(WOFilter));
intent.AddFlags(ActivityFlags.NewTask);
tspec = TabHost.NewTabSpec("Filter");
tspec.SetIndicator("Filter", Resources.GetDrawable(Resource.Drawable.Filter));
tspec.SetContent(intent);
TabHost.AddTab(tspec);
TabHost.TabChanged += (sender, e) =>
{
Toast.MakeText(this, TabHost.CurrentTab.ToString(), ToastLength.Short).Show();
};
}
catch (Exception ex)
{
Toast.MakeText(this, ex.InnerException.ToString(), ToastLength.Short);
}
}
You can use the TabHost.TabChanged event.
tabHost.TabChanged += (sender, e) => {
if (tabHost.CurrentTab == 0) {
// Do what you want.
}
};
PS: Xamarin Docs is your friend.
Edit:
You should modify your code to this...
//TabHost.TabChanged += TabHost_TabChanged;
TabHost.TabChanged += (sender, e) =>
{
Toast.MakeText(this, TabHost.CurrentTab.ToString(), ToastLength.Short).Show();
};
TabHost.CurrentTab is the index of the selected tab.