Abdul Mirza
Abdul Mirza

Reputation: 23

Tabbar disappears in custom renderer for android and toolbar is always null

I has a custom renderer for a tabbar in my android app and every time I apply it the tabs disappear. Also when I try to access my toolbar in the tabbed page it always returns null. But if I access it in my content page like so

var toolbar =(Android.Support.V7.Widget.Toolbar)MainActivity.RootFindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar); It works. What could be my issues?

TabbarDroid:

// Every time applied i lose my tabs
[assembly: ExportRenderer(typeof(BaseTabbedPage), typeof(Aura.SSC.MobileX.Droid.Renderers.TabbedPageRenderer))]
namespace Aura.SSC.MobileX.Droid.Renderers
{
public class TabbedPageRenderer : TabbedRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
    {
        base.OnElementChanged(e);
    }
}
}

Tabbar XML code:

<?xml version="1.0" encoding="utf-8" ?>
<views:BaseTabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
        xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
        x:Class="Aura.SSC.MobileX.Pages.MainPage2"
        xmlns:views="clr-namespace:Aura.SSC.MobileX.Views"
        xmlns:videos="clr-namespace:Aura.SSC.MobileX.Pages.Videos"
        xmlns:account="clr-namespace:Aura.SSC.MobileX.Pages.Account"
        xmlns:help="clr-namespace:Aura.SSC.MobileX.Pages.Help">

<TabbedPage.ToolbarItems>
    <ToolbarItem Icon="ic_launch.png" Clicked="ToolbarItem_Clicked"/>
</TabbedPage.ToolbarItems>

<TabbedPage.Children>
<views:BaseNavigationPage Title="Videos" Icon="ic_play_button.png">
    <x:Arguments>
        <videos:VideosMasterPage />
    </x:Arguments>
</views:BaseNavigationPage>


<views:BaseNavigationPage Title="Account" Icon="ic_account.png">
    <x:Arguments>
        <account:AccountsMasterPage />
    </x:Arguments>
</views:BaseNavigationPage>


<views:BaseNavigationPage Title="Help" Icon="ic_lifesaver.png">
    <x:Arguments>
        <help:HowToPage />
    </x:Arguments>
</views:BaseNavigationPage>

<views:BaseNavigationPage Title="Upload" Icon="ic_upload_button.png">
    <x:Arguments>
        <videos:UploadPage />
    </x:Arguments>
</views:BaseNavigationPage>
</TabbedPage.Children>

Tabbar.axml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.TabLayout 
xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"
                                     android:id="@+id/sliding_tabs"
                                     android:layout_width="match_parent"
                                     android:layout_height="wrap_content"
                                     android:background="?attr/colorPrimary"
                                     app:tabIndicatorColor="@android:color/white"
                                     app:tabMaxWidth="0dp"
                                     app:tabGravity="fill"
                                     app:tabMode="fixed" />

Upvotes: 0

Views: 417

Answers (1)

Billy Liu
Billy Liu

Reputation: 2168

I has a custom renderer for a tabbar in my android app and every time I apply it the tabs disappear.

You could try to use TabbedPageRenderer instead of TabbedRenderer.
For example:

public class MyTabbedPageRenderer : TabbedPageRenderer
{
    public MyTabbedPageRenderer(Context context) : base(context)
    {

    }            
}

Also when I try to access my toolbar in the tabbed page it always returns null.

The toolbar is null because it is never set in the activity. You need to set it first.

var toolbar = (Android.Support.V7.Widget.Toolbar)LayoutInflater.Inflate(Resource.Layout.Toolbar, null);
SetSupportActionBar(toolbar);

Also need use NavigationPage and modify the style.xml

MainPage = new NavigationPage(new BaseTabbedPage());

style.xml

<!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
<item name="windowNoTitle">true</item>
<!--We will be using the toolbar so no need to show ActionBar-->
<item name="windowActionBar">false</item>

Upvotes: 1

Related Questions