Change the color of the navigation bar - xamarin.forms

Iam developing a xamarin forms application.In my application,the navigation Bar color is blue. I want navigation bar color of one specific page as white.
I achieved that through
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.White;
Now Iam facing a problem.The page which have white navigationbar will pop back to previous page after certain time.After the popasync, previous pages which have blue navigation bar will also turn to white color. How to avoid that?.
namespace sample
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ApprovedAnimation : ContentPage
{
public ApprovedAnimation ()
{
InitializeComponent ();
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.White;
Device.StartTimer(TimeSpan.FromMilliseconds(3000), () =>
{
Navigation.PopAsync();
return false;
});
}
}
}

You need to set back the navigation color on the page itself using this method
protected override void OnDisappearing()
{
base. OnDisappearing();
((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Blue; // this will set back the normal color
}

Related

How to Change Status Bar Background, Text Color for Single Content Page Xamarin (Ios, Android)

How to Change Status Bar Background, Text Color for Single Content Page.
I add:
styles.xml
I think it works on all Content Pages
I want:
Page1: Status Bar Background is Green.
Page2: Status Bar Background is Red.
...
Thank you!
How to Change Status Bar Background, Text Color for Single Content
Page Xamarin (Ios, Android)
If you want to change the Status Bar Background color for single page, you can use DependencyService to achieve this.
1.In xamarin forms,add interface IStatusBarPlatformSpecific
public interface IStatusBarPlatformSpecific
{
void SetStatusBarColor(Color color);
}
2.In android,add class ChangeStatusbar to implement interface IStatusBarPlatformSpecific:
[assembly: Dependency(typeof(ChangeStatusbar))]
namespace ToastApp.Droid
{
public class ChangeStatusbar : IStatusBarPlatformSpecific
{
public ChangeStatusbar()
{
}
public void SetStatusBarColor(Xamarin.Forms.Color color)
{
// The SetStatusBarcolor is new since API 21
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
{
var androidColor = color.AddLuminosity(-0.1).ToAndroid();
//Just use the plugin
Xamarin.Essentials.Platform.CurrentActivity.Window.SetStatusBarColor(androidColor);
}
else
{
// Here you will just have to set your
// color in styles.xml file as shown below.
}
}
}
}
Note:Android only supports black or white icons & text.
For more, check:How to customize status bar icons and text color? E.g. status bar background: white, status bar icon color, and text: red
3.In IOS,create class ChangeStatusbarIOS to implement interface IStatusBarPlatformSpecific:
[assembly: Dependency(typeof(ChangeStatusbarIOS))]
namespace ToastApp.iOS
{
public class ChangeStatusbarIOS : IStatusBarPlatformSpecific
{
public void SetStatusBarColor(Color color)
{
UIView statusBar = new UIView(UIApplication.SharedApplication.KeyWindow.WindowScene.StatusBarManager.StatusBarFrame);
//statusBar.BackgroundColor = UIColor.Yellow;
statusBar.BackgroundColor = color.AddLuminosity(-0.1).ToUIColor();
UIApplication.SharedApplication.KeyWindow.AddSubview(statusBar);
}
}
}
4.In forms, you can change status backbround color for Single Content Page by override method OnAppearing and change back to it's original color by override method OnDisappearing:
protected override void OnAppearing()
{
base.OnAppearing();
var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
statusbar.SetStatusBarColor(Color.Green);
}
protected override void OnDisappearing()
{
base.OnDisappearing();
var statusbar = DependencyService.Get<IStatusBarPlatformSpecific>();
statusbar.SetStatusBarColor(Color.FromHex("3F51B5"));
}

SetIcon change color

Hello i am trying to set icon of bottom nav bar
Icon
But everytime it changes color of whole icon and then it look like this
Icon
Is it possible to not give icon color so it wont change whole icon?
You could try to use the custom renderer to the set the icon. And set the ItemIconTintList to null.
[assembly: ExportRenderer(typeof(AppShell), typeof(ShellCustomRenderer))]
namespace App_Shell.Droid
{
class ShellCustomRenderer : ShellRenderer
{
public ShellCustomRenderer(Context context) : base(context)
{
}
protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
return new CustomBottomNavAppearance();
}
}
public class CustomBottomNavAppearance : IShellBottomNavViewAppearanceTracker
{
public void Dispose()
{
}
public void ResetAppearance(BottomNavigationView bottomView)
{
}
public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
bottomView.ItemIconTintList = null;
IMenu myMenu = bottomView.Menu;
IMenuItem myItemOne = myMenu.GetItem(0);
if (myItemOne.IsChecked)
{
myItemOne.SetIcon(Resource.Drawable.cactus_24px); // selected icon
}
else
{
myItemOne.SetIcon(Resource.Drawable.icon_about); //default icon
}
}
}
}
Update:
If you want to change the burger icon of shell, you could use Custom renderer to reset the icon vis CreateToolbarAppearanceTracker.
For more details, you could refer to the thread i done before. How to hide back button in navigation bar using Xamarin.Forms - AppShell?
If you want to change the back button color of navigation bar, you could set in style. Please check the link below. Change back button color in Xamarin.Android

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

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)
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.
when you are working with xamarin forms it is suggested make use of common components and make least use of custom renderer.
Now for your requirement you want to create custom navigation bar
so here is how you can do it.
Create BaseContent Page
Create a Control Template inside your base page your can follow this link
Inside your control template using a grid view place your label with text binding (Back),also your can place a label in center to show title of page again u can make use of template binding which u would come to know when u go through the link
Now inherit your main page with your basecontentpage page
add your control template inside your main page
turn off your navigation bar of your main page
and you are done, this would give u more power to add more things like image or toolbar in your navbar
also to dynamically handle your back button u can check the count from navigationstack if its 0 u can show Humburger Icon or if its more than 0 u can show your label using IsVisible True/False

HOWTO: Manipulate Back Arrow Icon and Button on Navigation Bar (Xamarin Forms / Android)

I am attempting to change the size of the back arrow in the navigation bar.
Using information from this article, I have added the following code:
protected override Task<bool> OnPushAsync(Page view, bool animated)
{
var result = base.OnPushAsync(view, animated);
var activity = (Activity) Context;
var toolbar = activity.FindViewById<Toolbar>(Resource.Id.toolbar);
if (toolbar != null)
{
if (toolbar.NavigationIcon != null)
{
if (toolbar.NavigationIcon is DrawerArrowDrawable navigationIcon)
{
// Code goes here?
}
}
}
return result;
}
If this is indeed the correct path, what code goes in the area marked by the comment "Code goes here?"?
* UPDATE *
I realized that what I am trying to figure out was not exactly described in my original question.
More specifically, when I mentioned that I am trying to resize the navigation bar back arrow, what I am really trying to do is to resize the button that the icon appears on.
For example, if I shrink the height of the navigation bar using code like the following:
On<Android>().SetBarHeight(100);
The button that the icon appears on will be clipped.
Ultimately, what I am trying to accomplish is resizing the icon AND the button that the icon appears on. I have already figured out how to do the former.
I am attempting to change the size of the back arrow in the navigation bar.
If you want to change size of the back button in the navigation bar, you can get new icon from Drawable in resource for toolbar.NavigationIcon.
public class NavigationPageRenderer : Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer
{
public AppCompToolbar toolbar;
public Activity context;
protected override Task<bool> OnPushAsync(Page view, bool animated)
{
var retVal = base.OnPushAsync(view, animated);
context = (Activity)Forms.Context;
toolbar = context.FindViewById<AppCompToolbar>(Droid.Resource.Id.toolbar);
if (toolbar != null)
{
if (toolbar.NavigationIcon != null)
{
if (toolbar.NavigationIcon is DrawerArrowDrawable navigationIcon)
{
// Code goes here?
toolbar.NavigationIcon = Android.Support.V7.Content.Res.AppCompatResources.GetDrawable(context, Resource.Drawable.back);
toolbar.Title = "Back";
}
}
}
return retVal;
}
}
Here is the sample you can take a look:
https://github.com/hachi1030-Allen/XamarinCustomNavBar
<NavigationPage.TitleView> will help you out there. This is a XAML approach.
Example:
<NavigationPage.TitleView>
<StackLayout>
....
</StackLayout>
</NavigationPage.TitleView>
Using this approach you will be able to set HeightRequest and WidthRequest of whatever element you place inside the StackLayout and whatever else you want to amend.
Also, note that if you are having icon size problems it may be worth looking into whether or not your drawable/ icons are the right size for the right resolution.

Xamarin.Forms Add NavigationBar

I want to manage the nav bar appearence in the xaml?
And how can I manage the nav bar appearence in a Tabbed Page?
NavigationBar.SetHasNavigationBar(tabPage,true) seems not working.
Thank you all,
Ilenia
Maybe check that your mainpage is wrapped with NavigationPage in the public App() function:
MainPage = new NavigationPage(new LoginPage());
Changing the color of your navigation bar
To change the color of your navigation bar generically using Xamarin.Forms so that you only need to change the properties in one place, I'd suggest you add the following to your App.cs file like mentioned in this post on the Xamarin Forums.
Changing the color of the tab bar
Unfortunately, changing the color of the Tab Bar is currently not supported in xaml.
You will although need to write a customer renderer for iOS like so:
[assembly: ExportRenderer(typeof(TabbedPage), typeof(CustomTabbedPageRenderer))]
namespace JetAdvice_Free.iOS.Renderers
{
class CustomTabbedPageRenderer : TabbedRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
TabBar.TintColor = MonoTouch.UIKit.UIColor.Black;
TabBar.BarTintColor = MonoTouch.UIKit.UIColor.FromRGB(255, 128, 0);
}
}
}
Then, whenever you use a TappedPage on iOS, the background color will be applied to your tab bar.
You can set the navigationbar color only when you initialize your navigationpage like so:
new NavigationPage(your page here)
{
BarBackgroundColor = Color.Green,
BarTextColor = Color.White
};
For the issue of setting the tintcolor of the tabbar I recommend using a renderer like so:
public class TabPage_iOS : TabbedRenderer
{
public override void ViewWillAppear(bool animated)
{
base.ViewWillAppear(animated);
this.TabBar.TintColor = Color.Blue.ToUIColor();
UITableView tableView = (UITableView)this.MoreNavigationController.TopViewController.View;
tableView.TintColor = this.TabBar.TintColor;
}
}

Resources