21

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?

Ross
  • 46,186
  • 39
  • 120
  • 173
  • I'm dealing with exact same problem right now. Are you sure you can put theme in separate file? I tried to set theme to activity (instead of Application) and I also tried to apply it programmatically onStart - no luck... – katit May 24 '11 at 18:00
  • Hmmm. I think we have bigger problem. I tried to inherit from parent="android:Theme.Light.NoTitleBar.Fullscreen" and I got my activity FULL SCREEN. But my button and ListView styles still not applied.. So, it seems like theme "applied" but not all the item inside – katit May 24 '11 at 18:19

5 Answers5

23

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.

Olle Sjögren
  • 5,315
  • 3
  • 31
  • 51
Ana M
  • 467
  • 5
  • 13
4

I had to explicitly define theme for every <activity>.

delor
  • 800
  • 1
  • 7
  • 19
3

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.

Community
  • 1
  • 1
Paul Lammertsma
  • 37,593
  • 16
  • 136
  • 187
0

EDIT: I'm not sure if this was just a typo in the question, but in your main theme you reference Android's Theme.Black style incorrectly. Your code says:

<style name="Theme" parent="android:style/Theme.Black">

When it should say:

<style name="Theme" parent="@android:style/Theme.Black">

I feel like Lint or the compiler would bark at you for that before running the application, but maybe that will help.

dennisdrew
  • 4,399
  • 1
  • 25
  • 25
  • 1
    As long as your xml file is in the values folder everything should be fine, and the name of the file should not make a difference. – Rolf ツ Sep 25 '12 at 15:02
  • After looking over the official documentation, you are correct. However, I do spy another possible error. Updating now. – dennisdrew Sep 25 '12 at 16:13
0

Just out of curiosity. Try renaming your Theme to something else like FooBar.

Going to state the obvious here, but make sure your button is using the right style.

Richard Lalancette
  • 2,401
  • 24
  • 29