ActionBar and selected Tab

MattiaLeonardoMattiaLeonardo USMember
edited August 2014 in Xamarin.Android

Hello everyone,
this is my first post on this forum. I am quite a newbie in Android development and Xamarin platform, and I hope you can help me.

I have an activity with a shiny ActionBar. I need to create two Tabs (Tab0 and Tab1 from left to right), with default selection Tab1.
I thought this was quite easy, using the AddTab() method that allows to set the position as suggested in http://stackoverflow.com/questions/9895958/android-actionbar-tabs-set-initially-selected-tab.

I am having some problems: the code below works almost correctly (tabs are in the right position and I see fragment of the currently selected Tab1), but on the ActionBar Tab0 is selected (highlighted with the blue line, as the image attached). Selecting Tab0, the _tab0 fragment is loaded and viewed as it should be.

`

        //  Creating Tab1
        var Tab1 = this.ActionBar.NewTab();
        Tab1.SetText(Resource.String.Tab1);
        Tab1.SetIcon(Resource.Drawable.Tab1);
        Tab1.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
        {
            if (_tab1 == null)
            {
                _tab1 = new Tab1Fragment();
                e.FragmentTransaction.Add(Resource.Id.fragmentContainer, _tab1);
            }
            else
                e.FragmentTransaction.Show(_tab1);
        };
        Tab1.TabUnselected += delegate(object sender, ActionBar.TabEventArgs e)
        {
            e.FragmentTransaction.Hide(_tab1);
        };
        // Adding Tab1
        this.ActionBar.AddTab(Tab1, 0, true);

        // Creating Tab0
        var tab0 = this.ActionBar.NewTab();
        tab0.SetText(Resource.String.tab0);
        tab0.SetIcon(Resource.Drawable.tab0);
        tab0.TabSelected += delegate(object sender, ActionBar.TabEventArgs e)
        {
            if (_tab0 == null)
            {
                _tab0 = new Tab0Fragment();
                e.FragmentTransaction.Add(Resource.Id.fragmentContainer, _tab0);
            }
            else
                e.FragmentTransaction.Show(_tab0);
        };
        tab0.TabUnselected += delegate(object sender, ActionBar.TabEventArgs e)
        {
            e.FragmentTransaction.Hide(_tab0);
        };
        // Adding Tab0: this should make it in the left position, not selected
        this.ActionBar.AddTab(tab0, 0, false);

`

I also tried adding this.ActionBar.SetSelectedNavigationItem(1); at the end but it doesn't do anything (and I'm not surprised since Tab1 seems to be actually selected and showed, but not correctly highlighted on the ActionTab).

Am I doing something wrong? Thanks in advance for any help.

Sign In or Register to comment.