Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin forms: How to make tabs on bottom of the page?

I am looking for tabs which will be on the bottom part of the page. I tried the xamarin forms TabbedPage, but for ios only the tabs are coming bottom and for android and windows the tabs are showing on the top. Is there any way to achieve this feature?

like image 456
Sreejith Sree Avatar asked Oct 27 '18 11:10

Sreejith Sree


People also ask

How do I add a tab page to a content page in xamarin?

Now, right click on your portable project -> Add -> New item. Then, go to Cross-Platform -> Forms Tabbed Page Xaml (rename it) -> click Add. Now, you will see some code in your tabbed page. A tab page is a parent page that contains multiple pages in it.

What is carousel page?

The carousel is a slideshow for cycling through a series of content, built with CSS 3D transforms and a bit of JavaScript. It works with a series of images, text, or custom markup. It also includes support for previous/next controls and indicators.


1 Answers

Edit: for new Xamarin.Forms projects you should use Xamarin.Forms Shell, it provides easy tabs and other great features.

To accomplish that you have 3 options:

1) To use new bottom tabbar native control you must have Xamarin Forms version 3.1 or above.

On your tabbed page you need to add the android specification for bottom placemente:

XAML

<?xml version="1.0" encoding="utf-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
    android:TabbedPage.ToolbarPlacement="Bottom"
    x:Class="YouProjectNameSpace.MyTabbedPage">
</TabbedPage>

Or c# code behind

using System;
using Xamarin.Forms.PlatformConfiguration;
using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
using Xamarin.Forms;

namespace YouProjectNameSpace
{
    public partial class MyTabbedPage : TabbedPage
    {
        public MyTabbedPage()
        {
            InitializeComponent();
            On<Android>().SetToolbarPlacement(ToolbarPlacement.Bottom);
        }
    }
}

If you want more customization you can add:

<?xml version="1.0" encoding="utf-8"?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:android="clr-namespace:Xamarin.Forms.PlatformConfiguration.AndroidSpecific;assembly=Xamarin.Forms.Core"
    android:TabbedPage.ToolbarPlacement="Bottom"
    BarBackgroundColor="#2196F3"
    BarTextColor="White"
    android:TabbedPage.BarItemColor="#66FFFFFF"
    android:TabbedPage.BarSelectedItemColor="White"
    x:Class="YouProjectNameSpace.MyTabbedPage">
</TabbedPage>

2) Create a custom renderer for Android of the tabbed page and move it to the bottom

using System;
using Xamarin.Forms;

namespace YouProjectNameSpace
{
    public class CustomTabbedPage : TabbedPage
    {
    }
}

And the renderer:

using System;
using Android.Content;
using Android.Support.Design.Widget;
using Android.Support.V4.View;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android.AppCompat;

[assembly: ExportRenderer(typeof(CustomTabbedPage), typeof(CustomTabbedPageRenderer))]
namespace YouProjectNameSpace.Android
{
    public class CustomTabbedPageRenderer : TabbedPageRenderer
    {
        public CustomTabbedPageRenderer(Context context): base(context)
        {
        }

        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            this.InvertLayoutThroughScale();

            base.OnLayout(changed, l, t, r, b);
        }

        private void InvertLayoutThroughScale()
        {
            this.ViewGroup.ScaleY = -1;

            TabLayout tabLayout = null;
            ViewPager viewPager = null;

            for (int i = 0; i < ChildCount; ++i)
            {
                Android.Views.View view = GetChildAt(i);
                if (view is TabLayout tabs)
                    tabLayout = tabs;
                else if (view is ViewPager pager)
                    viewPager = pager;
            }

            tabLayout.ViewTreeObserver.AddOnGlobalLayoutListener(new GlobalLayoutListener(viewPager, tabLayout));

            tabLayout.ScaleY = viewPager.ScaleY = -1;
        }

        private class GlobalLayoutListener : Java.Lang.Object, Android.Views.ViewTreeObserver.IOnGlobalLayoutListener
        {
            private readonly ViewPager viewPager;
            private readonly TabLayout tabLayout;

            public GlobalLayoutListener(ViewPager viewPager, TabLayout tabLayout)
            {
                this.viewPager = viewPager;
                this.tabLayout = tabLayout;
            }

            public void OnGlobalLayout()
            {
                this.viewPager.SetPadding(0, -this.tabLayout.MeasuredHeight, 0, 0);
                this.tabLayout.ViewTreeObserver.RemoveOnGlobalLayoutListener(this);
            }
        }
    }
}

3) Use specific library like PXTabs, this one creates a full Xamarin Forms bottom tabs without any native code.

If you want to read more about bottom tabs and renderers:

Setting TabbedPage Toolbar Placement and Color

Xamarin.Forms: Official Bottom Navigation/Bottom Tabs on Android

BottomNavigationBarXF

like image 118
FabriBertani Avatar answered Jan 16 '23 13:01

FabriBertani