I have a theme that refuses to be applied to activities - none of the styles are applied. If I don't supply the layout_width
/layout_height
attributes for <Button>
it also gets a runtime error, showing that the Button
class isn't being applied.
/res/values/themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:style/Theme.Black">
<item name="android:windowNoTitle">true</item>
<item name="android:buttonStyle">@style/Button</item>
<item name="android:windowBackground">@color/page_background_light</item>
<item name="android:textAppearance">@style/TextAppearance</item>
</style>
</resources>
/res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TextAppearance" parent="@android:style/TextAppearance">
<item name="android:textSize">12sp</item>
<item name="android:textColor">@color/darkblue</item>
</style>
<style name="Button" parent="@android:style/Widget.Button">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<!--<item name="android:textColor">#3C2F4F</item>
<item name="android:textSize">20dip</item>-->
</style>
</resources>
and the relevant manifest setting:
<application android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/Theme">
What's the obvious mistake I'm missing?
In Android Studio, open themes. xml (app > res > values > themes > themes.
A theme is a collection of attributes that's applied to an entire app, activity, or view hierarchy—not just an individual view. When you apply a theme, every view in the app or activity applies each of the theme's attributes that it supports.
I've been struggling with this as well, and think I've finally found what may have been the problem - perhaps it is the same.
I had the custom theme defined in the res\values
folder, but not in the values-v11
and values-v14
folders. Which I think made it so that in some devices (specially the 2 I was testing with!), the theme could not be applied because it did not exist.
I now see the properties set in my custom theme (applied at the application level) taking effect.
I had to explicitly define theme for every <activity>
.
According to this answer, it appears that it isn't possible to provide layout_width
and layout_height
through styles. While it compiles, the exception it raises is:
E/AndroidRuntime(4117): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example/com.example.MainActivity}:
java.lang.RuntimeException: Binary XML file line #79: You must supply a
layout_width attribute.
I'm not sure why that is, but there might be a workaround. As this question suggests, you might be able provide a reference as the width and height parameter.
Again, my experiences are that Android doesn't properly support providing the widget dimensions through styles.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With