Home » Android » How does an android:attr style work?

How does an android:attr style work?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to build an Android UI via layouts. I start with the following:

<TextView
    android:id="@+id/..."
    android:layout_marginTop="8dip"
    android:text="..."
    style="?android:attr/listSeparatorTextViewStyle"/>

And that looks good (all caps, smaller font, dividing bar underneath it). Now I want to extend the style, so I change it to the following:

<TextView
    android:id="@+id/..."
    android:layout_marginTop="8dip"
    android:text="..."
    style="@style/section_title"/>

With a style of:

<style name="section_title" parent="@android:attr/listSeparatorTextViewStyle">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
</style>

And that doesn’t work (font is correct, but the divider line is gone).

How come… that?

How to&Answers:

When you’re using:

style="?android:attr/listSeparatorTextViewStyle"

you’re using the style pointed by this attribute(listSeparatorTextViewStyle). If you look in the platform themes.xml you’ll see that the style that is actually used for this attribute is Widget.TextView.ListSeparator.White. So this is the style you should extend in your custom style.

Unfortunately that style is private and you can’t extend it, or you shouldn’t extend it(for reference, see this bug report from google). Your best option would be to copy that entire style, Widget.TextView.ListSeparator.White(Widget.TextView.ListSeparator isn’t public as well so would have to also copy that), in your custom style and use that instead of extending the style from the android platform(see this response from the link above).