Search code examples
androidxamarinthemesandroid-themexamarin.forms

Xamarin Forms custom theme not working


I have an Android app that uses Xamarin Forms 2.0. I made a custom theme to set some colors. I created these files:

Resources/values/styles.xml (AndroidResource)

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="SmartbitLight" parent="SmartbitLight.Base">

  </style>

  <style name="SmartbitLight.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/blueLight</item>
    <item name="colorPrimaryDark">@color/blueDark</item>
    <item name="colorAccent">@color/grey</item>
    <item name="android:windowBackground">@color/white</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="android:statusBarColor">@color/blue</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
  </style>
</resources>

Resources/values-v21/styles.xml (AndroidResource)

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <style name="SmartbitLight" parent="SmartbitLight.Base">

  </style>
</resources>

Resources/values/colors.xml (AndroidResource)

<?xml version="1.0" encoding="utf-8" ?>
<resources>
  <color name="blueLight">#3cc1f1</color>
  <color name="blue">#33a4cd</color>
  <color name="blueDark">#33a4cd</color>
  <color name="grey">#7d7d7d</color>
  <color name="white">#ffffff</color>
</resources>

And then I have a MainActivity where I hook up this theme to the app:

[Activity(
    Label = "Smartbit", 
    Icon = "@drawable/icon", 
    MainLauncher = true, 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation,
    Theme = "@style/SmartbitLight")]
public class MainActivity : FormsAppCompatActivity

This doesn't work correctly though: the app bar is white instead of the intended blue, and the 'android:windowBackground' turns the text and border of my Entry control white as well.


Solution

  • I managed to get the custom theme working with all the colors for the appbar and everything. I created a toolbar.axml file and registered it in my FormsAppCompatActivity as described here: https://blog.xamarin.com/material-design-for-your-xamarin-forms-android-apps/.