Remove Android Action Bar Icon in Xamarin.Forms

In this article we are creating a custom control renderer for Xamarin.Forms allowing developers to override the default native rendering of a Xamarin.Forms control with their own platform-specific code.

We have seen many Xamarin related forums having a strong discussion about removing the action bar icon from an Android project in Xamarin.Forms. Many of them suggest making the application icon as transparent by altering the MainActivity.cs in Android project somewhat like the following:

[Activity (Label = "Sample Application", Icon = "@android:color/transparent", MainLauncher = true]

In this condition no one notices that by altering like this they are making the application icon to be transparent. As a result the application will not show an icon when it is installed on a device.

Today we are showing a way to render your page so that you will be able to remove the unwanted action bar icon depending on your needs.

Here I'm going to render the NavigationPage class that manages the navigation and user experience of a stack of other pages.

Create a class named CustomNavigationRenderer inside the Android project that will be a platform-specific implementation that contains code to hide the icon from the action bar.
  1. using Android.App;  
  2. using Android.Graphics.Drawables;  
  3. using Xamarin.Forms;  
  4. using Xamarin.Forms.Platform.Android;  
  5. using SampleApp;  
  6. using SampleApp.Droid;  
  7.   
  8. [assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationRenderer))]  
  9. namespace SampleApp.Droid {  
  10.     public class CustomNavigationRenderer: NavigationRenderer {  
  11.         protected override void OnElementChanged(ElementChangedEventArgs < NavigationPage > e) {  
  12.             base.OnElementChanged(e);  
  13.   
  14.             RemoveAppIconFromActionBar();  
  15.         }  
  16.         void RemoveAppIconFromActionBar() {  
  17.             var actionBar = ((Activity) Context).ActionBar;  
  18.             actionBar.SetIcon(new ColorDrawable(Color.Transparent.ToAndroid()));  
  19.         }  
  20.     }  
  21. }  

Now create a subclass of the NavigationPage control that is to be customized.
public class CustomNavigationRenderer : NavigationPage {}


Build and run your application successfully.


 






HAPPY CODING   (-_-)


Popular posts from this blog

Face Detection In Xamarin.Android

Floating Action Button for Xamarin.Android