3

The Problem:

I have several TextViews that share a large number of identical attributes. I'd like to apply the following style to all the TextViews.

<!-- TextView -->
<style name="fieldLabel" parent="@android:style/Widget.TextView">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">0.7</item>
    <item name="android:textSize">18sp</item>
</style>

But for some reason, when I apply this style as an activity-wide theme I get this error:

09-08 15:57:17.034: ERROR/AndroidRuntime(5269): Caused by: java.lang.RuntimeException: Binary XML file line #38: You must supply a layout_width attribute.

This is only happens when I try to apply this style as a theme. Because, when I add

style="@style/fieldLabel"

individually to every TextView's attributes it works as expected, so the error is coming from trying to apply this theme to the entire activity.


The Code:

styles.xml:

<style name="CustomActivityStyle" parent="android:Theme"> <!--This is theme I am trying to apply -->
    <item name="android:textViewStyle">@style/fieldLabel</item>
</style>

<!-- TextView -->
<style name="fieldLabel" parent="@android:style/Widget.TextView">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_weight">0.7</item>
    <item name="android:textSize">18sp</item>
</style>

myactivity.xml:

<!-- This is an example of one of the TextViews -->
<TextView
    android:id="@+id/name_label"
    android:text="@string/name_label"
/>

AndroidManifest.xml:

<!-- Implementing the theme in the Manifest -->
<activity android:name=".MyActivity"
    android:configChanges="orientation"
    android:theme="@style/CustomActivityStyle"
    android:label="@string/my_title">
</activity>

There is another thread on this issue here, but I decided to ask a new question instead of reviving the old one.

To Summarize:

I do know that I could easily make this work by simply adding the layout_width and layout_height attributes, or by explicitly adding the style="@style/fieldLabel" to each TextView. But the point of this question is more to improve my (and hopefully others) coding conventions, as well as making the code itself more readable and reusable.

I am actually very surprised that no one has come across this issue before, and that this is not more of a standard way of formatting.

Thanks for any help.

Community
  • 1
  • 1
Matt Garriott
  • 362
  • 2
  • 12
  • This is usually a layout related exception, can you post the full stacktrace and layout file. – Dan S Sep 08 '11 at 22:51
  • @Dan I've edited my original question to include the full stack trace and the entire layout file. Thanks for helping! – Matt Garriott Sep 09 '11 at 03:29

1 Answers1

-1

This is the problem:

Caused by: java.lang.RuntimeException: Binary XML file line #20: You must supply a layout_width attribute.

Your TextView id name_label, requires a android:layout_height and android:layout_width attribute.

Dan S
  • 9,139
  • 3
  • 37
  • 48
  • Yes, but I am supplying both a layout_height and a layout_width using a custom theme that should apply to all the textviews in the activity. So the question is, why are they not inheriting those fields correctly? – Matt Garriott Sep 09 '11 at 04:23
  • I've never seen anyone apply a height and width via style. Does each TextView need its own style attribute? – Dan S Sep 09 '11 at 05:02
  • 1
    If I explicitly add the style attribute to each TextView it will work, but I was hoping to avoid that by adding the style to the entire Activity using the android:theme attribute in the manifest. I don't see any reason why this should not work, thus my original question. Thanks for helping by the way! – Matt Garriott Sep 09 '11 at 14:01