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"
Related
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
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!
I know that
we have not to use parent attribute. We prefix one style to another
style separating by a period(.)
so in this style, does it have a circular inheritance?
<style name="TextAppearance.A" parent="TextAppearance.A.B">
<item name="android:textAlignment">viewStart</item>
<item name="android:gravity">start</item>
</style>
TextAppearance.A.B inherits from TextAppearance.A because of android dots' syntax.
but TextAppearance.A inherits from TextAppearance.A.B because of android paretn syntax.
Is it really a problem?
Technically As per Android Documentation I dont think this is possible,
Because this will lead to duplication of style, If you refer to same as Diamond Problem it will be one of those, also android prevents you from inheriting from more than one style.
Further Imagine if you have one attribute which is defined in style A also in Style B, it will be a problem at compile time that which attribute to choose from both.
For More Details please refer to android documentation
https://developer.android.com/guide/topics/ui/look-and-feel/themes
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
A create a simple Theme as
<style name='one'>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
However on viewing in the emulator the screen goes black.when i do not apply theme the screen has a white background .
what really happens here.i am just starting with android.
In addition ,if a apply a theme to my activity then the attributes of the theme applies to all components of my activity say button,textfields and edittexts .
why would i then write
android:textSize=?android:textSize
to reference value from the theme for any button in my layout when the same value would already be applying.
is the syntax above the correct way to reference an attribute from my theme to assign to attribute for any view in my layout.
thanks
tejinder
Yeah, so you need to do a little more reading.
Let's start with the basics,
You need to understand the differente betweent an Attribute, a Style, and a Theme.
An Attribute is something that can be styled. For instance: android:textSize is an attribute that can have any value.
A Style is a set of specific attributes that will be applied to a Widget. They are defined
in your /values/styles.xml
For instance:
<style name="normalTextThin" parent="android:Widget.Holo.Light.TextView">
<item name="android:gravity">left|center_vertical</item>
<item name="android:padding">8dp</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">16sp</item>
</style>
The styles can be applied either as part of a theme or directly as theme-independent.
Theme-indepentent styling of a widget is like this:
<TextView
android:id="#+id/text"
style="#style/normalTextThin"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
You are then theming only that one TextView.
A Theme is a collection of Styles that can be applied to a part of your UI, such a a whole Activity, or your whole Application.
For instance:
<style name="AppTheme" parent="android:Theme.Light">
<item name="android:editTextStyle">#style/EditTextAppTheme</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
</style>
Here, we are declaring that all EditText in your application will use the style named EditTextAppTheme, and so forth and on. When done like this, in order to actually have the theme be active, you declare it in the manifest:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
That means that you are not required to declare the style on each widget you create.
<EditText
android:id="#+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_search">
<requestFocus />
</EditText>
That widget right there would already be styled using EditTextAppTheme without the need of you explicitely declaring so.
I recommend you try to read on what attributes can be styled, how to style them, and so forth and on.
If you don't want to though, it's fine, you can still get a lot done with the following tools for styling:
ActionBarStyleGenerator to help you create styles for the ActionBar.
Android Holo Colors to help you style standard widgets.
Hope that helps.
Additional Info
Let me clarify on the whole ?attr/attributeName
The ? means that the system will choose the specific attributeName value for the current Configuration (not specific to different themes). This should be used only when you want the value to be different on different configurations. For example:
?android:attr/actionBarSize
This line is a dimension, and it will be different not based on the current theme, but on the current device screen size and orientation (values, values-land, values-sw600dp).
It's important to know that specifying ?android: means you are accessing preset Android values, not yours. If you have or want to create and use your own attribute values for specific configurations, you must do the following:
Create a file named attrs.xml on your /values/ folder.
Declare the desired custom attribute:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<attr name="my_custom_attr" format="reference" />
</resources>
Declare a value for the custom attribute, let's say on your own theme.
<style name="AppTheme" parent="android:Theme.Light">
<item name="my_custom_attr">#resource_type/resource_name</item>
<item name="android:editTextStyle">#style/EditTextAppTheme</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
<item name="android:imageButtonStyle">#style/ImageButtonAppTheme</item>
</style>
And then you can use it on the Widget you'd like:
Hope that clears things out.
EDIT 2
For a better answer to your question, please update your question. And like I said, read more on how to properly create styles.
The Theme named 'one', what do you want to apply it to? An activity, a Widget, the whole Application?
How are you applying the theme? Show the lines of code where you specify the usage of theme 'one'.
Your theme as you specified is simply not a properly constructed theme/style.
<style name='one'>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
This says absolutely nothing, and it is definitely not suitable for an Activity-level theme. The reason you specify a parent is so your theme can inherit all of the attributes from the parent, and then you specifiy which ones to change.
For instance, if you want to use your theme and have a light background, do this:
<style name='one' parent="android:Theme.Holo.Light>
<item name='android:textColor'>#eea</item>
<item name='android:textSize'>20sp</item>
</style>
But even here, despite the fact that it will apply, you don't want to have the same text color and size for the whole application do you? That'd be nonsense, different text color and sizes account for a big part of the user experience, so rather than setting those values from what we can refer to as the main style, we can create substyles and apply them to certain widgets.
I can't really go any more detailed that what I already have, the above explains how to accomplish Widget-specific styling, and activity/application level theming.
For a complete start-up guide, read the Android Developer Site, try the test styles declared there, see how they work, and until then try to create your own, don't try to create something out of nowhere if no reading has been made.