1

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?

McKabue
  • 2,076
  • 1
  • 19
  • 34
  • Seems like you did not create a custom theme to exclude the default action bar `false` : https://developer.xamarin.com/guides/android/user_interface/controls/tool-bar/part-1-replacing-the-action-bar/#custom_theme – SushiHangover Dec 24 '17 at 08:24
  • it is there in https://github.com/McKabue/CustomAndroidToolBar/blob/50839f8582e904d35e558cb21f9406a4e720db49/App3/Resources/values/styles.xml#L5 – McKabue Dec 24 '17 at 09:02
  • Not debugging offsite resources, but delete the apk from the emulator/device and perform a clean/rebuild – SushiHangover Dec 24 '17 at 09:16
  • it still returns the same 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. occurred` – McKabue Dec 24 '17 at 10:41
  • https://medium.com/google-developers/theming-with-appcompat-1a292b754b35 - TLDR; Use a `Theme.AppCompat.NoActionBar` theme if you are providing your own. – Jon Douglas Dec 24 '17 at 16:53

1 Answers1

1

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.

York Shen
  • 9,014
  • 1
  • 16
  • 40