Search code examples
androidxamarinxamarin.androidandroid-actionbarandroid-toolbar

Xamarin - Replacing the Action Bar (Android 7.1 - API 25)


I have tried creating a toolbar by replacing the default actionbar exactly as detailed in the documentation : Part 1 - Replacing the Action Bar, but the app doesn't run and throws an error at SetActionBar(toolbar);. The following is the error message:

Java.Lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set android:windowActionBar to false in your theme to use a Toolbar instead.

the following is the full error:

Unhandled Exception:
Java.Lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set android:windowActionBar to false in your theme to use a Toolbar instead. 

Here is the git repo of all my code : Github CustomAndroidToolBar

am using visual studio enterpise 2017, version 15.5

Where am i going wrong?


Solution

  • Xamarin - Replacing the Action Bar (Android 7.1 - API 25)

    There is some error in your project.

    First, please read this official sample, you are using some wrong item. You should use

    <item name="windowNoTitle"> 
    

    instead of

    <item name="android:windowNoTitle">. 
    

    Modify your style.xml like this :

    <!-- Base theme applied no matter what API -->
    <style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <!--We will be using the toolbar so no need to show ActionBar-->
        <item name="windowActionBar">false</item>
        <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
        <!-- colorPrimary is used for the default action bar background -->
        <item name="colorPrimary">#2196F3</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="colorPrimaryDark">#1976D2</item>
        <!-- colorAccent is used as the default value for colorControlActivated
         which is used to tint widgets -->
        <item name="colorAccent">#FF4081</item>
     </style>
    

    Second, Install Xamarin.Android.Support.v7.AppCompat nuget package :

    enter image description here

    Then extends AppCompatActivity instead of Activity for you MainActivity, and use a Theme.AppCompat theme (or descendant) with this activity.

    Please note that although you have written a custom theme in your project, you didn't use it for your MainActivity. You could read the document : Theming an Activity, add the theme for you MainActivity :

    [Activity(Label = "App3", MainLauncher = true, Theme = "@style/MyTheme")]
    public class MainActivity : AppCompatActivity
    {
        ...
    }
    

    Third, in your project you are using Android.Widget.Toolbar, please change it to Android.Support.V7.Widget.Toolbar.

    In your MainActivity :

    var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
    
    if (toolbar != null)
    {
        SetSupportActionBar(toolbar);
        SupportActionBar.Title = "Hello from Appcompat Toolbar";
    }
    

    In your toolbar.xml :

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    ...
    

    Then it works fine on my side.