1

I need to change the colors of the action bar, but when I change these data in the Android app, it does not change colors dynamically (unlike in iOS).

I need when I change the default color of the action bar to white (background color) the system will automatically change the title and return arrow colors to black (they are white and invisible). As I said above, in the iOS app only with this code the system already makes this switch automatically, however for Android I do not know how to do this same effect.

Follow the code below:

private void ChangeActionBarColor()
{
    var navigationPage = Detail as NavigationPage;
    navigationPage.BarBackgroundColor = Ambiente.Instance.NavigationColor;
    navigationPage.BarTextColor = Ambiente.Instance.NavigationItemColor;
    Title = Ambiente.Instance.NavigationTitle;
}
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Luiz Negrini
  • 656
  • 10
  • 32

1 Answers1

1

You need to create a custom theme in droid/resource/styles.xml. and also need to apply this theme in your MainActivity

Apply Theme on MainActivity

[Activity(Label = "YourProject.Droid", Icon = "@drawable/icon", Theme = "@style/MyTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

Add a custom theme in Style.XML file

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <style name="MyTheme" parent="MyTheme.Base">
    </style>
    <!-- Base theme applied no matter what API -->
    <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <!--If you are using revision 22.1 please use just windowNoTitle. Without android:-->
        <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>
        <!-- You can also set icon, Title color -->
        <item name="android:textColorPrimary">#000000</item>
        <!-- You can also set colorControlNormal, colorControlActivated
             colorControlHighlight and colorSwitchThumbNormal. -->
        <item name="windowActionModeOverlay">true</item>
        <item name="android:datePickerDialogTheme">@style/AppCompatDialogStyle</item>
        <item name="android:actionBarPopupTheme">@style/CustomActionBarPopupTheme</item>
    </style>
    <style name="AppCompatDialogStyle" parent="Theme.AppCompat.Light.Dialog">
        <item name="colorAccent">#FF4081</item>
    </style>
    <style name="CustomActionBarPopupTheme" parent="android:ThemeOverlay.Material.Light">
        <item name="android:colorBackground">#FFFFFF</item>
        <item name="android:textColor">#000000</item>
    </style>
</resources>

For more understanding

enter image description here

sme
  • 4,023
  • 3
  • 28
  • 42
Pavan V Parekh
  • 1,906
  • 2
  • 19
  • 36