Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Tabbed Page with Icon

$
0
0

Hello,

I want to set icons in tabbed page and I'm using this code but everytime i get null exception on this line if (actionBar.TabCount > 0) and also app crash when i go from login screen to this main screen https://forums.xamarin.com/discussion/comment/151236/#Comment_151236

Code is
`public partial class Main : TabbedPage {
public Main() {
InitializeComponent();
NavigationPage.SetHasNavigationBar(this, false);

        if (Device.OS == TargetPlatform.iOS)
        {
            Children.Add(new NavigationPage(new Timer() { Title = "Ikas" }));
            Children.Add(new NavigationPage(new Views.Reports.ReportsPage() { Title = "Dkas" }));
            Children.Add(new NavigationPage(new Profile() { Title = "Zkas" }));
        }
        else
        {
            Children.Add(new Timer() { Title = "Ikas", Icon = "ic_ikas" });
            Children.Add(new Views.Reports.ReportsPage() { Title = "Dkas", Icon = "ic_dkas" });
            Children.Add(new Profile() { Title = "Zkas", Icon = "ic_zkas" });
        }
    }

}`

and custom renderer

using Android.App; using Android.Widget; using Xamarin.Forms; using Xamarin.Forms.Platform.Android; using Android.Graphics.Drawables; using System; using Android.Graphics; using Android.Runtime; using System.ComponentModel; using Android.Views;
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CTabbedPageRenderer))]
namespace somenamespace
{
public class (CTabbedPageRenderer))]: TabbedRenderer
{

`    private Activity activity;
    private TabbedPage _tabbedPage;
    private const string COLOR = "#FFFFFF";


    protected override void OnElementChanged(ElementChangedEventArgs<TabbedPage> e)
    {
        base.OnElementChanged(e);

        activity = this.Context as Activity;
        _tabbedPage = e.NewElement as TabbedPage;


    }

    protected override void DispatchDraw(Canvas canvas)
    {

        ActionBar actionBar = activity.ActionBar;


        if (actionBar.TabCount > 0)
        {
            ColorDrawable colorDrawable = new ColorDrawable(global::Android.Graphics.Color.ParseColor(COLOR));
            actionBar.SetStackedBackgroundDrawable(colorDrawable);
            ActionBarTabsSetup(actionBar);
        }

        base.DispatchDraw(canvas);
    }

    private void ActionBarTabsSetup(ActionBar actionBar)
    {
        try
        {
            //_tabbedPage.Children[0].IC
            for (int i = 0; i < actionBar.TabCount; i++)
            {
                Android.App.ActionBar.Tab dashboardTab = actionBar.GetTabAt(i);
                if (TabIsEmpty(dashboardTab))
                {

                    int id = Resources.GetIdentifier(_tabbedPage.Children[i].Icon.File, "drawable", Context.PackageName);
                    TabSetup(dashboardTab, id);
                }

            }

        }
        catch (Exception)
        {

        }

    }

    private bool TabIsEmpty(ActionBar.Tab tab)
    {
        if (tab != null)
            if (tab.CustomView == null)
                return true;
        return false;
    }

    private void TabSetup(ActionBar.Tab tab, int resourceID)
    {
        ImageView iv = new ImageView(activity);
        iv.SetImageResource(resourceID);
        iv.SetPadding(0, 10, 0, 0);

        tab.SetCustomView(iv);
    }
}

}`
Can some one help me?


Viewing all articles
Browse latest Browse all 77050

Trending Articles