PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 12, 2022

[FIXED] How could I change the Navigastion's page arrow in Xamarin Forms?

 September 12, 2022     cross-platform, xamarin.forms     No comments   

Issue

I'm creating an app using xamarin Forms (multiplatform), I'm using a Navigation page, but I want to change the arrow ("<-") to text ("back")

Do you know how could i do it? Thanks

(I'm going to use it in an Android App, but I'm creating the app using Xamarin forms)


Solution

You could use custom renderer to remove the navigation icon and set it with text. But, when you do that, you need to capture the click of the text and simulate the back event.

Create the interface:

public class CustomNavigationPage : NavigationPage
{
    public CustomNavigationPage(Page startupPage) : base(startupPage)
    {

    }
}

The implementation of Android:

  [assembly: ExportRenderer(typeof(CustomNavigationPage), 
typeof(NavigationPageRenderer_Droid))]
namespace NavigationPageDemo.Droid
{
public class NavigationPageRenderer_Droid : NavigationPageRenderer
{
    public Android.Support.V7.Widget.Toolbar toolbar;
    public Activity context;
    public NavigationPageRenderer_Droid(Context context) : base(context)
    {

    }
    protected override Task<bool> OnPushAsync(Page view, bool animated)
    {
        var retVal = base.OnPushAsync(view, animated);

        context = (Activity)Forms.Context;
        toolbar = context.FindViewById<Android.Support.V7.Widget.Toolbar>(Droid.Resource.Id.toolbar);

        if (toolbar != null)
        {
            //if (toolbar.NavigationIcon != null)
            //{
            //toolbar.NavigationIcon = Android.Support.V7.Content.Res.AppCompatResources.GetDrawable(context, Resource.Drawable.back);
            //toolbar.NavigationIcon = null;

            toolbar.NavigationIcon = null;
            toolbar.Title = "back";
            toolbar.SetOnClickListener(new OnClick());
        


            //}
        }

        return retVal;
    }

    protected override Task<bool> OnPopViewAsync(Page page, bool animated)
    {
        return base.OnPopViewAsync(page, animated);
    }

}
public class OnClick : Java.Lang.Object, IOnClickListener
{
    void IOnClickListener.OnClick(Android.Views.View v)
    {
        App.Current.MainPage.Navigation.PopAsync();
    }

  
}

In the custom renderer, use the OnClickListener to capture the click on text.

enter image description here



Answered By - Wendy Zang - MSFT
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing