How to Detect Which Tab was Pressed in ActionBar.TabHost ?

hexagodhexagod Member ✭✭✭

Does anyone know how to detect which tab was pressed in ActionBar.TabHost? I'm trying to use

        TabHost.TabWidget.GetChildAt(0).SetOnClickListener(new MyOnClickListener(tabHost));

and I've got a MyOnClickListener class

public class MyOnClickListener : Java.Lang.Object, IOnClickListener
{
    TabHost tabHost;
    public MyOnClickListener(TabHost tabHost)
    {
        this.tabHost = tabHost;
    }
    public void OnClick(View v)
    {
        WebView subWebView = tabHost.CurrentView.FindViewById<WebView>(Resource.Id.webViewSubs);
        if (subWebView != null)
        {
            subWebView.Reload();
        }
    }
}

but I need a way to check which tab the user has pressed. I've been researching this for a while and can't find an answer. I know how to check which tab is selected, but not how to detect which tab the user has actually clicked on. Is there an event that I can use? I want to add logic for my click listener so that I can check the tab selected event and fire different CurrentTab = as-well as load different WebViews.

Best Answer

  • hexagodhexagod ✭✭✭
    Accepted Answer

    Answer was this @BillyLiu couldn't have done it without your help my man =]

    //Assemblies

    using System;
    using System.Threading.Tasks;
    using Android.App;
    using Android.Content;
    using Android.Graphics.Drawables;
    using Android.OS;
    using Android.Widget;
    using Android.Webkit;
    using static Android.Widget.TabHost;
    using static Android.Views.View;
    using Android.Views;

    //this assembly is your Activities class, which should contain your class instances
    using com.xamarin.example.BitChute.Activities;

    Put these click listeners inside your MainActivity.cs .. the int inside GetChildAt() represents the tab our listener will respond to. int 0 = tab farthest left and they go up 0 .. 1 .. 2 .. etc .. 1 would mean tab second from the left. Each listener will need it's own listener class instance. I'll show two examples in the next block.

    TabHost tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
    
    tabHost.Setup();
    
    tabHost.TabWidget.GetChildAt(0).SetOnClickListener(new WhatsOnClickListener(tabHost));
    
    tabHost.TabWidget.GetChildAt(1).SetOnClickListener(new SubsClickListener(tabHost));
    
    tabHost.TabWidget.GetChildAt(2).SetOnClickListener(new DiscoverClickListener(tabHost));
    
    tabHost.TabWidget.GetChildAt(3).SetOnClickListener(new MyChannelClickListener(tabHost));
    
    tabHost.TabWidget.GetChildAt(4).SetOnClickListener(new SettingsClickListener(tabHost));
    

    Create a separate ClickActivity.cs file and then add these listeners into it. you will need to create an OnClickListener instance for each tab. This example is only two of the listeners; but if you're using my template, you will need to use these examples to create another 3 listeners.

    public class WhatsOnClickListener : Java.Lang.Object, IOnClickListener
    {
    TabHost tabHost;

    //this int will tell the click listener whether to reload the webview or pop 2 root
    static int tbC = 0;

    public WhatsOnClickListener(TabHost tabHost)
    {
    //tell the clicklistener which tabhost to use
    this.tabHost = tabHost;
    }

    //this class handles the click event
    public void OnClick(View v)
    {

    //declare the webview and tell our object where to find the XAML resource
    WebView webViewWhatsOn = tabHost.CurrentView.FindViewById<WebView>(Resource.Id.webViewWhatsOn);
    
    //...if the CurrentTab != 0 ... we won't fire the Reload() or LoadUrl() 
    //..without this logic, the app will crash because our WebViews 
    //.aren't set to an instance of an object
    if (tabHost.CurrentTab == 0)
    {
        //if tbC int is 0, we will reload the page
        if (tbC == 0)
        {
            //tell whatsOnWebView to Reload
            webViewWhatsOn.Reload();
    
            //set the int to one so next time webview will pop to root
            tbC = 1;
        }
        //else if the int is 1, we will pop to root on tab 0
        else if (tbC == 1)
        {
            //tell whatsOnWebView to pop to root
            webViewWhatsOn.LoadUrl(@"https://bitchute.com/");
    
            //set the tbC int so that next time webview will reload
            tbC = 0;
        }
    }
    
    //if our current tab isn't zero, we need to set CurrentTab to 0
    //this line is critical or our tabs won't work when not selected
    tabHost.CurrentTab = 0;
    

    }

    }

    public class SubsClickListener : Java.Lang.Object, IOnClickListener
    {
    TabHost tabHost1;

    static int tbC = 0;

    public SubsClickListener(TabHost tabHost1)
    {
    this.tabHost1 = tabHost1;
    }
    public void OnClick(View v)
    {
    if (tabHost1.CurrentTab == 1)
    {

        WebView subWebView = tabHost1.CurrentView.FindViewById<WebView>(Resource.Id.webViewSubs);
    
        if (tbC == 0)
        {
            subWebView.Reload();
    
            tbC = 1;
        }
    
        else if (tbC == 1)
        {
            subWebView.LoadUrl(@"https://bitchute.com/subscriptions/");
    
            tbC = 0;
        }
    }
    
    tabHost1.CurrentTab = 1;
    

    }

    }

Answers

  • hexagodhexagod Member ✭✭✭

    I found this for Java... can't test it @tm tho and I'm using C#

    //JAVA
    final String myTabTag = "My Tab";
    final int myTabIndex = 3;

    getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );

    getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
    getTabHost().setCurrentTab(myTabIndex );
    }
    }
    });
    so.... maybe...

    String myTabTag = "My Tab";
    int myTabIndex = 3;

    getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );

       TabHost.TabWidget.GetChildAt(0).SetOnClickListener(new MyOnClickListener(tabHost));
    
    public void OnClick(View v)
    {
            if (getTabHost().getCurrentTabTag().equals(myTabTag)) // <<<< How to convert into xamarin?
            {
                  TabHost.CurrentTab = myTabIndex;
            }
     }
    

    ???

    I've got the whole thing worked out... I think... except for this line

    if (getTabHost().getCurrentTabTag().equals(myTabTag))

    prolly something like

    if (TabHost.GetCurrentTab().ToString() == myTabIndex.ToString()) // ???
    {
    //do something ?
    }

  • hexagodhexagod Member ✭✭✭

    prolly something like

    if (TabHost.GetCurrentTab().ToString() == myTabIndex.ToString()) // ???
    {
    //do something ?
    }

    if (TabHost.GetClickedTab().ToString() == myTabIndex.ToString()) // ???**

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @hexagod

    Have you tried to create different MyOnClickListener for your tabs?
    For example, use MyFirstClickListener for your first tab, and MySecondClickListener for the second tab.

  • hexagodhexagod Member ✭✭✭
    Accepted Answer

    Answer was this @BillyLiu couldn't have done it without your help my man =]

    //Assemblies

    using System;
    using System.Threading.Tasks;
    using Android.App;
    using Android.Content;
    using Android.Graphics.Drawables;
    using Android.OS;
    using Android.Widget;
    using Android.Webkit;
    using static Android.Widget.TabHost;
    using static Android.Views.View;
    using Android.Views;

    //this assembly is your Activities class, which should contain your class instances
    using com.xamarin.example.BitChute.Activities;

    Put these click listeners inside your MainActivity.cs .. the int inside GetChildAt() represents the tab our listener will respond to. int 0 = tab farthest left and they go up 0 .. 1 .. 2 .. etc .. 1 would mean tab second from the left. Each listener will need it's own listener class instance. I'll show two examples in the next block.

    TabHost tabHost = FindViewById<TabHost>(Android.Resource.Id.TabHost);
    
    tabHost.Setup();
    
    tabHost.TabWidget.GetChildAt(0).SetOnClickListener(new WhatsOnClickListener(tabHost));
    
    tabHost.TabWidget.GetChildAt(1).SetOnClickListener(new SubsClickListener(tabHost));
    
    tabHost.TabWidget.GetChildAt(2).SetOnClickListener(new DiscoverClickListener(tabHost));
    
    tabHost.TabWidget.GetChildAt(3).SetOnClickListener(new MyChannelClickListener(tabHost));
    
    tabHost.TabWidget.GetChildAt(4).SetOnClickListener(new SettingsClickListener(tabHost));
    

    Create a separate ClickActivity.cs file and then add these listeners into it. you will need to create an OnClickListener instance for each tab. This example is only two of the listeners; but if you're using my template, you will need to use these examples to create another 3 listeners.

    public class WhatsOnClickListener : Java.Lang.Object, IOnClickListener
    {
    TabHost tabHost;

    //this int will tell the click listener whether to reload the webview or pop 2 root
    static int tbC = 0;

    public WhatsOnClickListener(TabHost tabHost)
    {
    //tell the clicklistener which tabhost to use
    this.tabHost = tabHost;
    }

    //this class handles the click event
    public void OnClick(View v)
    {

    //declare the webview and tell our object where to find the XAML resource
    WebView webViewWhatsOn = tabHost.CurrentView.FindViewById<WebView>(Resource.Id.webViewWhatsOn);
    
    //...if the CurrentTab != 0 ... we won't fire the Reload() or LoadUrl() 
    //..without this logic, the app will crash because our WebViews 
    //.aren't set to an instance of an object
    if (tabHost.CurrentTab == 0)
    {
        //if tbC int is 0, we will reload the page
        if (tbC == 0)
        {
            //tell whatsOnWebView to Reload
            webViewWhatsOn.Reload();
    
            //set the int to one so next time webview will pop to root
            tbC = 1;
        }
        //else if the int is 1, we will pop to root on tab 0
        else if (tbC == 1)
        {
            //tell whatsOnWebView to pop to root
            webViewWhatsOn.LoadUrl(@"https://bitchute.com/");
    
            //set the tbC int so that next time webview will reload
            tbC = 0;
        }
    }
    
    //if our current tab isn't zero, we need to set CurrentTab to 0
    //this line is critical or our tabs won't work when not selected
    tabHost.CurrentTab = 0;
    

    }

    }

    public class SubsClickListener : Java.Lang.Object, IOnClickListener
    {
    TabHost tabHost1;

    static int tbC = 0;

    public SubsClickListener(TabHost tabHost1)
    {
    this.tabHost1 = tabHost1;
    }
    public void OnClick(View v)
    {
    if (tabHost1.CurrentTab == 1)
    {

        WebView subWebView = tabHost1.CurrentView.FindViewById<WebView>(Resource.Id.webViewSubs);
    
        if (tbC == 0)
        {
            subWebView.Reload();
    
            tbC = 1;
        }
    
        else if (tbC == 1)
        {
            subWebView.LoadUrl(@"https://bitchute.com/subscriptions/");
    
            tbC = 0;
        }
    }
    
    tabHost1.CurrentTab = 1;
    

    }

    }

  • hexagodhexagod Member ✭✭✭

    @BillyLiu said:
    @hexagod

    Have you tried to create different MyOnClickListener for your tabs?
    For example, use MyFirstClickListener for your first tab, and MySecondClickListener for the second tab.

    yep... the answer to this literally came to me in a dream on thursday night of last week. Your advice gave me enough to figure the rest of it out. thanks for checking tho! Appreciate your help @BillyLiu

Sign In or Register to comment.