0

I would like to ask how to go about changing the shape of tabs in Xamarin for tabbed pages for android application. I know that this would obviously require a custom renderer, however which class do I inherit from? Do I inherit from the TabbedPageRenderer or the TabbedRenderer? Also, how do I know which function calls the rendering of the Tabs so that I can override it?

Mr Ask
  • 57
  • 9

1 Answers1

1

I know that this would obviously require a custom renderer, however which class do I inherit from? Do I inherit from the TabbedPageRenderer or the TabbedRenderer?

If you want to change the shape of tabs, then you need to inerit your custom renderer from TabbedPageRenderer. Because only TabbedPageRenderer has SetTabIcon to override, which allows you to access current tab object.

Also, how do I know which function calls the rendering of the Tabs so that I can override it?

What you need to override is the SetTabIcon method. In this method, you are able to set the custom view of current tab.

Notes: In order to let SetTabIcon get called, you will need to set the Icon of every sub page, otherwise SetTabIcon won't get called.

So, you can follow below steps to change the shape of your tabs:

  1. Create a custom TabbedPage in PCL:

    public class MyTabbedPage:TabbedPage
    {
    }
    
  2. Use it in your Page:

    <local:MyTabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:local="clr-namespace:CustomTabbedPageDemo"
                 x:Class="CustomTabbedPageDemo.MainPage">
        <!--Icon needs to be set in order to call SetTabIcon-->
        <local:Page1 Title="Page One" Icon="icon.png"/>
        <local:Page2 Title="Page Two" Icon="icon.png"/>
    </local:MyTabbedPage>
    
  3. Create a .axml view file in Resource/layout:

    <?xml version="1.0" encoding="utf-8"?>
    <View xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@drawable/myshape"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></View>
    
  4. Define a custom shape xml file(MyShape.xml) in Resource/drawable, you can define any kind of shape you wanted:

    <?xml version="1.0" encoding="utf-8" ?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
      <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF"
          android:angle="270"/>
    </shape>
    
  5. Create Custom Renderer for your custom tabbed page, and override SetTabIcon:

    [assembly:ExportRenderer(typeof(MyTabbedPage),
     typeof(MyTabbedPageRenderer))]
    namespace CustomTabbedPageDemo.Droid
    {
        public class MyTabbedPageRenderer:TabbedPageRenderer
        {
    
            protected override void SetTabIcon(TabLayout.Tab tab, FileImageSource icon)
            {
                base.SetTabIcon(tab, icon);
                tab.SetCustomView(Resource.Layout.tab_view);
            }
        }
    }
    
  6. Optionally, you can remove the tabbar's background image by removing android:background of Resource/Tabbar.axml's TabLayout:

    <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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:tabIndicatorColor="@android:color/white"
        app:tabGravity="fill"
        app:tabMode="fixed" />
    

Here is the complete demo, you can refer to:CustomTabbedPageDemo.

Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24
  • Thanks but I would like to change the shape of the tab if possible and not just the icon on the tab. I would like to change them to custom shapes with almost no gap in between. Something similar to puzzle pieces fitted together and I would also like to have text titles shown on them. Do I still modify TabbedPageRenderer? – Mr Ask Jun 22 '17 at 09:10
  • My sample didn't change the icon, but change the entire view of the tab by `tab.SetCustomView(Resource.Layout.tab_view);`. You can use any view in `tab_view`. – Elvis Xia - MSFT Jun 22 '17 at 09:17
  • any idea how to remove the gaps between the tabs and also how to remove the underline? – Mr Ask Jun 22 '17 at 09:18
  • By `Something similar to puzzle pieces fitted together and I would also like to have text titles shown on them.`, it is not possible to use android view to accomplish that, not even in native android apps, you can refer to [this thread](https://stackoverflow.com/questions/17201960/shape-of-puzzle-for-custom-view/17202410#17202410) for explaination. – Elvis Xia - MSFT Jun 22 '17 at 09:19
  • Aww. It would be good if it can be done especially since Xamarin allows for custom rendering. But thanks alot :D – Mr Ask Jun 22 '17 at 09:24
  • @MrAsk removing the underline is possible, you can accomplish that by setting `app:tabIndicatorHeight="0dp"` to `android.support.design.widget.TabLayout` mentioned in step 6. – Elvis Xia - MSFT Jun 22 '17 at 09:29
  • Sorry I'm gonna ask this since I am new to android but how do you place your picture in an axml file? I created an axml file but it seems that all i can do is change its background color – Mr Ask Jun 22 '17 at 09:52
  • Use ImageView to [display an Image](https://developer.xamarin.com/recipes/android/controls/imageview/display_an_image/). – Elvis Xia - MSFT Jun 23 '17 at 00:36