Style Inheritance


#1

I don’t understand what the book is saying:

I tried this, but it doesn’t work:

<style name="RemoteButton.boldButoon" > <item name="android:textColor">#aa0000</item> <item name="android:textStyle">bold</item> </style>

Then I just copied what the default styles.xml file already does for its two styles, which works:

<style name="boldButoon" parent="RemoteButton"> <item name="android:textColor">#aa0000</item> <item name="android:textStyle">bold</item> </style>

In general, I am finding the android documentation impossible to figure out, when it comes to doing things in code. There is no TextView.setTextStyle() command. There is a TextView.setTextAppearance(), which may allow you to set the bold attribute:

[quote] setTextAppearance(Context context, int resid)
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.[/quote]

Good luck figuring out what the “specified TextAppearance” is supposed to be though. Are there static fields defined in some class somewhere you use?

Searching brings up the R.attr class, which has:

[quote]public static final int textAppearance
Added in API level 1

Default appearance of text: color, typeface, size, and style.

Must be a reference to another resource, in the form “@[+][package:]type:name” or to a theme attribute in the form “?[package:][type:]name”.
Constant Value: 16842804 (0x01010034)[/quote]

Please explain this better. That is why I am reading a book instead of trying to learn it from the online documentation. I want the author to de-mystify the online docs.

Thanks.


#2

[quote=“RickDroid”]I don’t understand what the book is saying:

I tried this, but it doesn’t work:

<style name="RemoteButton.boldButoon" > <item name="android:textColor">#aa0000</item> <item name="android:textStyle">bold</item> </style>
[/quote]

I just tried your code myself in the solution’s code. Works fine on my 4.2 emulator. Hmm…

Styles are applied at inflation time. In general, it is not possible to change them once a view is inflated.

[quote]There is a TextView.setTextAppearance(), which may allow you to set the bold attribute:

[quote] setTextAppearance(Context context, int resid)
Sets the text color, size, style, hint color, and highlight color from the specified TextAppearance resource.[/quote]

Good luck figuring out what the “specified TextAppearance” is supposed to be though.[/quote]

…with the exception of this handy, poorly documented method, which I did not previously know about. Thank you!

It appears to take in a style resource, from which it will attempt to pull the following attributes:

-textColorHighlight
-textColor
-textSize
-textColorHint
-textColorLink
-fontFamily
-typeface
-textStyle
-textAllCaps

I make no guarantees on which versions the above applies to.


#3

Well, for the Nerdlauncher I wanted to create a LinerarLayout and add an ImageView and TextView programatically. I succeeded except for the properties of the TextView. The text was at the top. I found one command to change it, but I got a Lint error that it was added in API 17.

Trying to figure out what commands are available and what goes in them as parameters was difficult. It would appear that the Android developers are only now making such things available anyway.


#4

I added a new entry into values\styles.xml as below:

<style name="RemoteButton">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">match_parent</item>
</style>
<style name="ActionButton" parent="RemoteButton">
    <item name="android:textStyle">bold</item>
</style>

Then I assigned the ‘ActionButton’ style to the ‘enter’ and ‘delete’ buttons in RemoteControlFragment.onCreateView():

...
Button enterButton = (Button)bottomRow.getChildAt(2);
enterButton.setText("Enter");

enterButton.setTextAppearance(getActivity(), R.style.ActionButton);

enterButton.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v){
....

Works fine on my HTC Desire w/ICS device.
Hope it helps,
Devon


#5

FWIW, here’s the way I approached it:

styles.xml[code]

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

<style name="RemoteButton">
    <item name="android:layout_width">0dp</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:background">@color/fragment_background_color</item>
    <item name="android:layout_margin">@dimen/fragment_layout_margin</item>
    <item name="android:textColor">@color/fragment_text_color</item>
    <item name="android:textSize">@dimen/fragment_button_text_size</item>
</style>

<style name="RemoteButton.ControlButton">
    <item name="android:textSize">@dimen/fragment_controlButton_text_size</item>
    <item name="android:textStyle">bold</item>
</style>
[/code]

fragment_remote_control.xml
(inside the otherwise unchanged TableLayout - I just change the last

    <include
        android:layout_weight="1"
        layout="@layout/button_row" />

    <include
        android:layout_weight="1"
        layout="@layout/button_row" />

    <include
        android:layout_weight="1"
        layout="@layout/button_row" />

    <include
        android:layout_weight="1"
        layout="@layout/button_control_row" />[/code]

As you can see, I created (copied & edited from the button_row.xml) a button_control_row.xml, as follows:
[code]
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" >

    <Button style="@style/RemoteButton.ControlButton" />

    <Button style="@style/RemoteButton" />

    <Button style="@style/RemoteButton.ControlButton" />

</TableRow>
[/code]

I also edited the dimens.xml and colors.xml, just to try to develop good habits.  I wanted the font size larger on the numbers, and smaller on the controls. 
Here's the dimens.xml:[code]
<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">8dp</dimen>
    <dimen name="activity_vertical_margin">8dp</dimen>
    <dimen name="fragment_controlButton_text_size">24sp</dimen>
    <dimen name="fragment_button_text_size">36sp</dimen>
    <dimen name="fragment_layout_margin">3dp</dimen>


</resources>

#6

[quote=“clyder”]FWIW, here’s the way I approached it:

[/quote]

That’s how I approached it too.
Good to know that I wasn’t alone in my thinking.


#7

@clyder, I did it a similar way, although Devon11’s idea of changing the style in code is much more elegant, especially if the enter/delete buttons might change place in future versions of the app.

  1. Create a new style for the bottom row in res/styles as RemoteButton.Bold
    <style name="RemoteButton.Bold">
    	<item name="android:textStyle">bold</item>
    </style>
  1. Create button_row_last.xml in res/layout
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" >
    <Button style="@style/RemoteButton.Bold" />
    <Button style="@style/RemoteButton" />
    <Button style="@style/RemoteButton.Bold" />
</TableRow>
  1. Include button_row_last as the last row of the TableRow in fragment_remote_control.xml
...
  <include
        android:layout_weight="1"
        layout="@layout/button_row" />
    <include
        android:layout_weight="1"
        layout="@layout/button_row" />
    <include
        android:layout_weight="1"
        layout="@layout/button_row" />
    <include
        android:layout_weight="1"
        layout="@layout/button_row_last" />

</TableLayout>