Android XML attributes - android

I've been learning Android for quite some time now, but still can't understand why these are equal in a resource file:
this
<style name="MyTheme" parent="android:Theme">
and this:
<style name="MyTheme" parent="#android:style/Theme">
this
<item name="windowTitleSize">40dip</item>
and this:
<item name="android:windowTitleSize">40dip</item>
I can guess about the second one: "android" is the default package. No idea about the first one though.
Any help would be greatly appreciated!

I think the best explanation to this question could be found here.
If i understood this correctly it is basically two ways to refer to a resource, either in R class or XML. The values should be the same though.

Related

Activity Layout goes blank when switched to Material Design components

Problem :
There was no problem when app was using
Theme.MaterialComponents.Light.NoActionBar
But was not using any MDC elements in Layout like (TextInputLayout etc).
Then i replaced Editext with TextInputLayout and Button with Material Button.
Now here the problem comes.
When i run my app, i see the screen goes blank. Just pure white.
I checked Layout isses.
I found there're 7 warnings and 2 errors(Render problem).
The Render problems are as -
Path.op() not supported. (As was it suggests i refreshed the layout many times. Doesn't work)
java.awt.geom.IllegalPathStateException: missing initial move to in path definition
This is what i have tried -
Downgrading my MDC dependency (how to solve render problem Path.op() not supported?)
Clean project, rebuild and restart Android studio
Please help me to solve this strange issue.
I want to use MDC in my project.
Thank you.
I would suggest you should first make use of the Bridge then, gradually override the parent theme attributes based on your requirement design.
In AndroidManifest.xml
android:theme="#style/MyMaterialTheme"
Then under style.xml. it would like something like
<style name="MyMaterialTheme" parent="Theme.MaterialComponents.Light.NoActionBar.Bridge">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorPrimary</item>
<item name="android:windowBackground">#color/white</item>
<item name="toolbarStyle">#style/BlueToolbar</item>
</style>
try referring reference , reference
Simple solution is make your theme extend a Bridge theme :) I hope this helps

Layout Attributes within Styles

I've been with a dilemma for a while that I don't know how to solve it properly. I want to use DRY (Don't Repeat Yourself), but not apply bad practices in styles (such as set the layout attributes inside them).
This is my case...
To have the text styles encapsulated in my projects, I usually use the following:
I have a style called Wrap_Content
<style name="WrapContent">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
On the one hand, I have a style called Tv that inherits from WrapContent:
<style name="Tv" parent="WrapContent">
<item name="android:fontFamily">#font/font_foo</item>
<item name="android:textColor">#color/color_foo</item>
</style>
As you can see, apart, the Tv style has a default font and text color
If for example I want to use a font size of 15sp, I apply this style:
<style name="Tv.15">
<item name="android:textSize">15sp</item>
</style>
And so on...
Well, the issue is that all the TextView of my project I set wrap_content both width and height.
Therefore, doing things like this simplifies the layouts XML a lot and it increases the readability and grouping common behaviors.
Example:
<TextView
style="#style/Tv.15"
android:text="#string/foo"/>
And if in any case, I want to change any attribute, I have only to overwrite it from where I call it.
The dilemma is that I am mixing textAppearance styles with layout ones. I have thought about separating this ... but I have not just resolved the main issue, that I am setting layout attributes on it, something that I should know nothing more than its own view, and not its container.
But what does not convince me at all is to do something like this:
<style name="Tv">
<item name="android:fontFamily">#font/font_foo</item>
<item name="android:textColor">#color/color_foo</item>
</style>
<style name="Tv.15">
<item name="android:textSize">15sp</item>
</style>
<TextView
style="#style/Tv.15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/foo"/>
I don't want to repeat a million times with the same attributes if these are common. Or yes I see what it brings ... technical debt. Therefore, it does not seem like a valid option.
I have searched quite a lot and the truth is that I have not found anything that convinces me and I would like to reach something elegant, since it is something that I use at all times and I don't like it.
Well... what do you think about it?
Thank you so much!!!
EDITED 2019-11-08
I have thought a new approach adding a new layer of styles, the #style/TextAppearance. It is like this:
<style name="WrapContent">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="TextAppearance">
<item name="android:fontFamily">#font/font_foo</item>
<item name="android:textColor">#color/color_foo</item>
</style>
<style name="TextAppearance.15">
<item name="android:textSize">15sp</item>
</style>
<style name="Tv" parent="WrapContent">
<item name="android:textAppearance">#style/TextAppearance</item>
</style>
<style name="Tv.15">
<item name="android:textAppearance">#style/TextAppearance.15</item>
</style>
This add a little bit of complexity to the system, but it splits the layout and the textAppearance attributes. Moreover, it allows use the TextAppearance style for buttons, editTexts an so on.
In our most recent Android Dev Summit, two of my colleagues gave a talk on how to use Theme & Style. We advice that you use Themes for View groups and their children and styles for simpler views. Perhaps your layout needs can be met by using Themes and then reserving styles for text appearances and such. Beyond that, efficacy should guide how you structure your style objects.

What do i have to do to make a style for an activity that just affects the textviews?

Actually i have two questions.
Question 1.
I have made a style for a TextView
http://pastebin.com/q9hj26JX (Couldn't paste xml code here, it just went invisible)
To add this style i do:
http://pastebin.com/QdGmjQ0z
But instead of doint this, there must be a way to add this style to all the TextView in an activity? I have seen something like "Widget.TextView", but i have not found any good tutorial or documentation on it yet.
So can someone please give me an example, if it is possible.
Now for question number 2:
I don't get any intellisense while creating styles. Does it not exist for style creation?
Thanks in advance!
Please red about THEMES in android http://developer.android.com/guide/topics/ui/themes.html
The THEME is a style for whole activity and sets in Android Manifest.
Hope, it help you!
UPDATE:
Try this code:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="small_describing_text" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_marginTop">0dp</item>
<item name="android:textSize">10dp</item>
<item name="android:textColor">#FF0000</item>
</style>
<style name="MyTheme" parent="android:Theme.Light">
<item name="android:textViewStyle">#style/small_describing_text</item>
</style>
</resources>
Don't forget add this theme in Manifest for your activity!!!
You might want to have a look at my blogpost where I've explained theming and styling: http://aproblemlikemaria.wordpress.com/2011/09/26/theming-and-styling-in-android/

How to access style.xml values

I have styles.xml with Style definition like this:
<style name="Style1">
<item name="titleColor">#color/red</item>
<item name="lineColor">#color/light_red</item>
</style>
I'd like to access "titleColor", "lineColor" attributes values programmaticaly. Is it possible somehow to do?
Would appreciate your help very much, cause already spend hours trying to find a solution.
The answer is here how to get Theme attributes values
Yes, do it like that:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Style1">
<item name="titleColor">#color/red</item>
<item name="lineColor">#color/light_red</item>
</style>
</resources>
Then Build the Project, after Building you can access the values with
R.style.Style1...
edit to clarify:
button1.setBackgroundColor(R.style.Style1.titleColor);
I think you can use following code in order to access the style1,
style="#style1/titleColor"
style="#style1/lineColor"
That is all.

How to applicate android styles without change .java?

My question is simple, I want to improve an android application using styles, so without change the *.java file…what I mean is use always “android.R.layout“ in the .java, but be allowed to change the appearance of spinner (and its options), the color of a textview, the background…etc
I’ve researched that defining styles.xml like this:
<resources>
<style name="GreenHeader" >
<item name="android:textColor">#04B404</item>
<item name="android:typeface">normal</item>
<item name="android:textSize">30pt</item>
<item name="android:gravity">center</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="RedHeader" parent="#style/GreenHeader" >
<item name="android:textColor">#FF0000</item>
</style>
</resources>
and calling
:android:theme="#style/RedHeader"
in the manifest.xml, when you use (for example) in the layout:
<Spinner
style= "#style/RedHeader"
something change…but it doesn’t works well, I don’t know what more I need, maybe some other xml file called themes…but I don’t know, I’ve found hours and hours without result
I need help to improve it, please!! Could anyone help me???
Sorry you can not change styles programatically in android, but you can change a view or widget background programatically if it is enough for you.

Categories

Resources