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.
Related
I'm new to Android studio and I am wondering where to set the default font family for Android app in Android studio after reading the accepted answer here.
I have tried to put the code in activity_main.xml and AndroidManifest.xml but it does not work.
Any help would be appreciated!
Thanks KDeogharkar!
Looks like I would have to add the code inside of styles.xml in values folder. It worked :)
/res/values*/themes.xml
Pick any parent theme to extend here
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:textViewStyle">#style/RobotoFont</item>
</style>
<style name="RobotoFont" parent="android:Widget.TextView">
<item name="android:fontFamily">sans-serif</item>
</style>
</resources>
Then in your app manifest
android:theme="#style/AppTheme`"
I'm studying how to use custom theme in android application. After I create a style.xml, and input below xml strings.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#android:style/Theme.Light">
<item name="editTextColor">#00f</item>
</style>
</resources>
Eclipse gave an error when I run the project, it can not find the attribute "editTextColor", but in the sdkpath\platforms\android-17\data\res\values\themes.xml, it does use "editTextColor" attribute.
When I changed the editTextColor, the application works.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#android:style/Theme.Light">
<item name="android:editTextColor">#00f</item>
</style>
</resources>
Can anyone tell me why please? Thank you very much.
While inside sdkpath\platforms\android-17\data\res\values\themes.xml, it is referencing the value directly means inside android platform itself, but while you are trying to reference it inside your application you have to reference it using android:editTextColor because you are using it outside android platform itself and overriding its value.Hope you got some idea.
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.
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/
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.