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
Related
As a beginner in Android development, I'm currently creating a calculator application.
The purpose
I would like to create a specific style applicable to several elements has we can do with CSS class in web development.
The Style will must be applicable to the nine numerics buttons of the calculator (1 to 9).
To avoid repeating the same attributes 9 times (once time per "Button" element), I've created a "Style" block where I've put all the attributes commons of each button.
I've added this <style> in a XML resource file named "styles.xml", the content of this file is well loaded by the application because don't have any problems with the properties "dimen" and "colors" declared in this file.
Here's the code:
<!-- Style of the calculator numerics buttons -->
<dimen name="calculatorNumberWidth">60dp</dimen>
<dimen name="calculatorNumberHeight">60dp</dimen>
<style name="calculatorNumberStyle" parent="TextAppearance.AppCompat">
<item name="android:layout_width">90dp</item>
<item name="android:layout_height">90dp</item>
<item name="android:gravity">center</item>
<item name="backgroundTint">#F1FAEE</item>
<item name="android:textColor">#000000</item>
<item name="android:textSize">36dp</item>
<item name="layout_constraintBottom_toBottomOf">parent</item>
<item name="layout_constraintRight_toRightOf">parent</item>
<item name="layout_constraintTop_toTopOf">parent</item>
</style>
My problem:
I can't apply this style. I've tried two methods which aren't functional:
By adding the attribute android:style="#style/calculatorNumberStyle" in the "Button" tag of each button.
In this case, the compiler stops the build and raises this error: AAPT: error: attribute android:style not found
By adding the attribute android:theme="#style/calculatorNumberStyle" in the "Button" tag of each button.
In this case, the application is well compiled but the style is not applied.
I've tried to find my way throw this documentation: https://developer.android.com/guide/topics/ui/look-and-feel/themes, but without success.
Does anybody know which is the good method?
SDK platform version: 11.0, AndroidStudio version: 4.1
Thanks by advance and good day at all,
Mickaël
Just try to use style without android directive
style="#style/calculatorNumberStyle"
I am new to android and have a question about the styles.xml file.
Each item for example this
<item name="android:background">#color/black</item>
works both with "android:background" and with just "background".
What is the android: prefix there for?
When creating your own styles, you should always extend an existing style from the framework or support library so that you maintain compatibility with platform UI styles. To extend a style, specify the style you want to extend with the parent attribute. You can then override the inherited style attributes and add new ones.
For example, you can inherit the Android platform's default text appearance and modify it as follows:
<style name="GreenText" parent="#android:style/TextAppearance">
<item name="android:textColor">#00FF00</item>
</style>
However, you should always inherit your core app styles from the Android Support Library.
But if you don't inherit them from the android support library then too things will work the same!
Source: Explanation text and code taken from: https://developer.android.com/guide/topics/ui/look-and-feel/themes
You can read more about Styles and Themes from the above doc, from where I took the example to explain you!
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"... />
I’m new to android and are looking at tutorials to learn android development. I'm trying to figure out how all the xml files and tags are fit together.
In a google tutorial for actionbars one custom style, in themes.xml, look like this:
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Widget.Holo.ActionBar">
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
</style>
I don't find or understand where "#style/MyActionBarTitleText" is defined and i don't get any compiling error in Eclipse. When a tag is referenced like this i thought it has to be defined in a xml file under my project somewhere but i cant find it?
Style XMLs are defined in res/drawable[-mdpi/ldpi/xhdpi]
Style defines look of your Activity. You can define color, themes, shapes in Styles. Read more about styles and other resources here
http://developer.android.com/guide/topics/resources/style-resource.html
http://developer.android.com/guide/topics/resources/available-resources.html
Yes you can find it in: res->values->styles.xml
and also you can change your application theme/style in styles.xml file
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.