Leon Cullens
Leon Cullens

Reputation: 12476

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.

Upvotes: 2

Views: 4184

Answers (2)

Leon Cullens
Leon Cullens

Reputation: 12476

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/.

Upvotes: 0

Try adding the name of your theme (SmartbitLight) in the manifest, as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.test">
    <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application android:label="test" android:theme="@style/SmartbitLight"></application>
</manifest>

path: your_app / Droid / Properties / AndroidManifest.xml

Upvotes: 0

Related Questions