Android Article - How to  Inherit a Style
GoTo: Make Your Own Android App Free | View Android Articles

How To Inherit a Style

In our android tutorials you learned about styles; and how to use them to style your text in the TextView.


In this article you can learn the method used to 'inherit a style'. Like a style, they are referenced in the TextView element code, and added to the styles.xml page.

You can inherit a style by coding them into your code as follows:

We have 3 style attributes we want to add to our TextView text:
2 are added to the TextView element as shown; textColor and typeface.

TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="@string/themes"

The third style we want to add is textAppearance:Medium; To include the textAppearance.Medium style we will use the 'parent' code, which lets us inherit it, then we can just add our other 2 style attributes..


In order to use this method, the style you inherit must be a system predefined style such as textAppearance.Medium.

Here's the code:

STYLE
style name="CodeFont" parent="@android:style/TextAppearance.Medium"
item name="android:textColor" #00FF00
item name="android:typeface= "monospace"
STYLE

We name our customized inherited style 'CodeFont'. You can give it any name you want.
Now just add this code to the style.xml page; then add the line of code that references your style in the TextView element, as shown here.

android:textAppearance="@style/CodeFont"

Must create the page, style.xml file in res/value (same place as strings.xml) The style.xml maybe created for you when you create your APP using AIDE. Newer version of AIDE does create the page. For info on how to create the style.xml page, goto our "Styles Tutorial"

Save the files once you have coded them.

(get the actual code to copy/paste from our Example Code section this page)


Now RUN your app, to see the new styles we added to the TextView text.

The android system applies the styles: textAppearnce Medium,textColor #00ff00, and typeface monospace.



android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/CodeFont"
android:text="@string/themes"


When You Use Inherit a Style

- inherit using parent
- only with predefined styles, not ones you customize yourself
- then just add your customized styles to it
- In TextView, make reference to the 'parent style'.
All your customized styles are then included and applied to your TextView.




This is EXAMPLE CODE for coding 'inherit a style'.





Inherit Style API

Inherit style was added in API 1; therefore you don't need any additional support libraries added to your code.
Read about APIs and Android version compatibility

For additional reading
TextView style properties and attributes/values

You May Also Like These Android Articles: Android App Libraries | What is a WebView- Android App Coding


If you Like Our Free Android Articles - Give Us a Social Mention Share