Customize a TextView (text appearance) in Android Studio (Kotlin) - android

bear with me as I am new to android studio with the Kotlin language.
I have declared a textView object in my activity_main.xml file. I am trying to customise the font, font size, and text colour of this textView. I have been able to do so by customising each attribute individually; however, this is quickly getting tedious for my other activity.xml files.
I know that there is a textAppearance property that holds all these values. How can I declare a custom textAppearence so that I no longer need to change these values independently.

You can define custom styles in a styles.xml file located at: res/values/styles.xml
<style name="GreenText">
<item name="android:textColor">#00FF00</item>
</style>
It can be applied via:
<TextView style="#style/GreenText"... />

Related

How to add custom fonts globally

How to change custom fonts in android studio? It seems like there is no easy way to do such a simple thing. I see that you can change the font of TextView but to change the font for a particular element, drawer or globally I can't seem to find an explenation.
Just Like #tycj said in the comments, unfortunately there is no way to set global fonts to the Textviews. But you can set custom type-styles and apply them as "textAppearance" to the TextViews.
For instance: Assume you have such a style in your project
<style name="MyAPP.TextAppearance.Headline1" parent="TextAppearance.MaterialComponents.Headline1">
<item name="fontFamily">#font/your_font</item>
<item name="android:textSize">48sp</item>
</style>
Then , you simply apply this to your theme.Like this :
<item name="textAppearanceHeadline1">#style/MyAPP.TextAppearance.Headline1</item>
Now you can free to use this in a TextView but as the name of the style suggest, it is better to use this for the hearder TextViews in your app.
<TextView
...
android:textAppearance="?attr/textAppearanceHeadline1" />
the good thing is that you can override these attributes
<TextView
...
android:textAppearance="?attr/textAppearanceHeadline1"
android:textSize="36sp" />
To get further information, you can check here. Also I suggest checking this sample project as well

Android built-in styles

I'm a learner of Android programming and I'm currently reading this book, well it's the first, HeadFirst Android Development.
In chapter 14 Navigation Drawers, there was this attribute of TextView textAppearance that was given a value of #style/textAppearance.AppCompat.Body1.
The book said it was a built-in styles that makes text look slightly bolder.
My question is, how many built-in styles does Android has?
I want to know all of them.
If you go through the following link you will get all the styling options for TextView.
https://developer.android.com/reference/android/widget/TextView
you can check the styling options for other views from the left panel of the same link by clicking on other classes.
A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under res/values/ directory of your project and will have as the root node which is mandatory for the style file.
You can define multiple styles per file using tag but each style will have its name that uniquely identifies the style. Android style attributes are set using tag as:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomFontStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:capitalize">characters</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">12pt</item>
<item name="android:textColor">#00FF00</item>/>
</style>
</resources>
Using Styles
Once your style is defined, you can use it in your XML Layout file using style attribute as follows
<TextView
android:id="#+id/text_id"
style="#style/CustomFontStyle"
android:text="#string/hello_world" />
Always, when you are writing in XML to find the built-in attribute use:
attribute_name="#android:attribute_value
The #android: will list all the built-in the attribute for a given attribute you want, for Example:
style="#android:style/TextAppearance.AppCompat.Medium"
Default Styles & Themes
Android provides a large collection of styles and themes that you can use in your applications. You can find a reference of all available styles in the R.style class. To use the styles listed here, replace all underscores in the style name with a period. For example, you can apply the Theme_NoTitleBar theme with "#android:style/Theme.NoTitleBar".
You can see the following source code for Android styles and themes- here and here

What is difference among styles.xml and themes.xml

May I know what is the difference between styles.xml and themes.xml? To me, they just look same as both XML are in the same format.
<style name="...
<item name="...
So, in my app which provide customization coloring, size, drawable, ... do I need both styles.xml and themes.xml as well? How should I decide which XML to put in which file?
Out of the whole page of the Styles and Themes. You may be looking for this line.
When you apply a style to a single View in the layout, the properties
defined by the style are applied only to that View. If a style is
applied to a ViewGroup, the child View elements will not inherit the
style properties—only the element to which you directly apply the
style will apply its properties. However, you can apply a style so
that it applies to all View elements—by applying the style as a theme.
When you apply as theme, it changes everything in scope, depending if you applied it on Activity or Application. Style is more 'local'.
Quoted from Android API guide:
To create a set of styles, save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder.
The root node of the XML file must be <resources>.
Full documentation
So I guess it really doesn't matter if you put any styles in any files as long as it's an xml file which locates in res/values/ folder.
There is no functional difference between styles.xml and themes.xml as many answers have indicated.
It is worth noting that Google's iosched2014 app has ONLY a styles.xml (no themes.xml).
https://github.com/google/iosched
Taken from the Styles and Themes document, in the Defining Styles section:
To create a set of styles, save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder.
The root node of the XML file must be <resources>.
I am just realizing that for example Toolbar text colors go into a theme declaration, and for other aspects go into a style declaration
<style name="ToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
<style name="ToolbarStyle" parent="Base.Widget.AppCompat.Toolbar">
<item name="android:minHeight">?attr/actionBarSize</item>
<item name="android:background">#color/colorPrimary</item>
</style>
And in the toolbar declaration
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/ToolbarStyle"
android:theme="#style/ToolbarTheme">
</android.support.v7.widget.Toolbar>
I tried to omit the theme by moving its items into the style, but that didn't work. So in this case it seems as these are just styles which are as theme since they affect the inherited children in the toolbar such as the textfields.
Hi here is an article regarding to the difference between styles and themes XML in android please go through
And also here the android documentation regarding to styles and themes in Android.

Android style descriptions

I tried changing the appearance of a spinner and I partly succeeded. I'm doing this via overriding parts of the theme. I managed to change the text size of the spinner item (i.e. the text size in the drop down button) with my themes.xml and styles.xml:
My themes.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="#android:Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:spinnerItemStyle">#style/CustomSpinnerItem</item>
</style>
</resources>
My styles.xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomSpinnerItem" parent="#android:Widget.TextView.SpinnerItem">
<item name="android:textAppearance">#style/CustomTextAppearance</item>
</style>
<style name="CustomTextAppearance">
<item name="android:textSize">30dp</item>
</style>
</resources>
However I cannot find the attributes that are responsible for the text appearance of the items in the dropdown list of the spinner. I tried dropDownItemStyle amongst other things. In my opinion the attribute names are not self-explanatory, so I wanted to know whether there is a documentation of what attribute does what in a style to find out which attributes to override. I found it very cumbersome to trace back all the styles used in a theme via the themes.xml and styles.xml of the platfrom and then try to find the right attributes via trial and error.
I know that one can change the appearance by passing layouts to the adapter, however, this is not really what I was looking for, since (as far as I know), you can only use inheritance in styles and not in layout xml files. If I created a custom layout for the adapter I'd have to create 9-patch images etc., which I think is a bit too time consuming in case I only want to change the text size.
Of course it's possible that I misunderstood the whole concept, since I'm new to Android ;)
You probably have found out the answer since you asked but for others looking at similar questions:
I do not know of a list of attribute names with good explanation of what they do (R.attr's page mostly gives information that is already in the name) but the way I do it is:
Start from the element I give to setDropDownViewResource(), in my case: android.R.layout.simple_spinner_dropdown_item and find.
Find its layout definition in \sdk\platforms\android-17 (specific platform version to avoid redundant results).
Get its style from the layout file. In this case: ?android:attr/spinnerDropDownItemStyle
We now have the attribute name we need.
It's better to do it that way rather than try to guess what attribute to use because you know which attribute the system itself use so it's very likely to be the correct one (unless there's a bug).
If I created a custom layout for the adapter I'd have to create
9-patch images etc.
Well, no, the layout determines what kind of GUI element you would have (a textfield, a spinner, an imagebutton, a custom element...), not how they are styled (nine-patch backgrounds, text colors...), so you still would have to mess with styles to get the right appearance.
For example, for visual consistency I ported the button, checkbox and spinner style from Theme.Holo to Gingerbread, yet I did not mess with layout, all I did was the aforementioned steps plus looking up the result (spinnerDropDownItemStyle in the above example) in themes.xml, which gave me the style name (e.g.: Widget.Holo.DropDownItem.Spinner).
Then I looked that up in styles.xml and imported it (and any parent*) in my project's styles.xml, searching and copying any Holo specific reference in my project and adjusting the namespace accordingly (add android: to attributes and replace ?android:attr with #style for what I copy to my styles.xml file).
So far I haven't had to mess with layouts at all (even the presence of radio buttons in spinner dialogs on Gingerbread is determined by an xml attribute: android:checkMark).
If a style has no parent attribute (like Widget.Holo.DropDownItem.Spinner) then its parent is the same style minus the last element (e.g.: Widget.Holo.DropDownItem)

Styling all TextViews(or custom view) without adding the style attribute to every TextView

Is there a way to format all TextViews, Buttons or whatever with a theme ?
Like in CSS when i want to format all a-tags
a{
/some css here/
}
I want to do that in android via xml when I'm applying a theme to my application.
Any ideas ?Thanks
http://bartinger.at/
Update 1.0:
I want to create a theme that formats the text in all TextViews green and in all EditTexts red. So that i just apply the theme and I never have to worry about the style attribute!
Update 1.1:
So I found some that piece of code and I think that's a good beginning
<item name="android:textViewStyle">#style/MyTextView</item>
<item name="android:buttonStyle">#style/MyButton</item>
I think thats the answer to my question. But I have another one. I want to write my own ActionBar and wanted to know how I can apply a default style or default attributes (again without adding the style attribute in the layout xml :P )
I have a class
public class ActionBar extends LinearLayout{ }
and I'm gonna use it like that in my application
<at.bartinger.uil.ActionBar>....</at.bartinger.uil.ActionBar>
The ActionBar should have some default attributes (like height and width) and then adding some custom style attributes which could change from app to app (like background)
yes you can you can apply a theme to the whole application and then all your textviews will have that style.
Inside the styles.xml file you have to define your CustomTheme
for example:
<style name="CustomTheme" parent="android:Theme.Light">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
you add something like text
"android:textStyle="myStyle" and specify the details in Mystyle
You can apply a style read more here.
for the action abr you should look here http://developer.android.com/guide/topics/ui/actionbar.html
especially at the bottom it explains very well how to style the bar

Categories

Resources