Home » Android » android – Remove Icon from Action Bar xamarin

android – Remove Icon from Action Bar xamarin

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to remove my icon from action bar, I’m using a MasterDetail page to create a slider, I’m using the left icon to open the slider, but the right smaller icon sided of “APP Vendas” I want to remove, I tryied everything on C# to easly remove without success.
Tryiend tranpatent, RGBA, None, etc, here is my code and Image:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using App5.Menu;
using App5.Categorias;
using App5;
namespace App5
{
    public class MainPage : MasterDetailPage
    {
        public MainPage()
        {
            var menuPage = new MenuPage();
            menuPage.OnMenuTap = (page) =>
            {
                IsPresented = false;
                Detail = new NavigationPage(page);
            };
            Master = menuPage;
            Detail = new NavigationPage(new Resumo())
            {
                BarBackgroundColor = Color.Black,
                Icon = None
            };




        }

    }
}

The right Icon is what I want to remove

How to&Answers:

We can make 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.

So the best way is 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.

using Android.App;  
using Android.Graphics.Drawables;  
using Xamarin.Forms;  
using Xamarin.Forms.Platform.Android;  
using SampleApp;  
using SampleApp.Droid;  

[assembly: ExportRenderer(typeof(NavigationPage), typeof(CustomNavigationRenderer))]  
namespace SampleApp.Droid {  
    public class CustomNavigationRenderer: NavigationRenderer {  
        protected override void OnElementChanged(ElementChangedEventArgs < NavigationPage > e) {  
            base.OnElementChanged(e);  

            RemoveAppIconFromActionBar();  
        }  
        void RemoveAppIconFromActionBar() {  
            var actionBar = ((Activity) Context).ActionBar;  
            actionBar.SetIcon(new ColorDrawable(Color.Transparent.ToAndroid()));  
        }  
    }  
} 

Now create a subclass of the NavigationPage control that is to be customized.

public class CustomNavigationRenderer : NavigationPage {}

Build and run your application successfully.

Refer :

Answer:

You need to just add those single line code in MainActivity.cs file in your Android project.

ActionBar.SetIcon(Android.Resource.Color.Transparent);

For Example

using Android.OS;
using Android.Content.PM;

namespace Test.Droid
{

    [Activity(Label = "test", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            global::Xamarin.Forms.Forms.Init(this, bundle);
            ActionBar.SetIcon(Android.Resource.Color.Transparent);
            LoadApplication(new App());

        }
    }