I have a problem while creating styles in android:
When I use
android:textAppearance="?android:attr/textAppearanceSmall"
in Button for instance, that's working fine, but if I create a styles.xml like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="smalltext" parent="#style/Text">
<item name="android:textSize">10sp</item>
<item name="android:textColor">#FFF</item>
<item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
</style>
</resources>
and replace in Button:
style="#style/smalltext"
nothing is taken into account.
Would you have any idea of what could be wrong ?
thanks a lot,
Luc
Keep in mind that styles will not be applied in the layout view in Eclipse. You'll have to deploy the application to a device (or the emulator) to see the changes.
Related
Is there a way how to generate style from XML attributes so I don't have to manually copy paste 1 by 1? When I prototype, I use the "inline" styles then I want to export styles into styles.xml but manually it is time consuming task. It is just different format, should be easy to automate.
<ImageView
android:layout_width="80dp"
android:layout_height="47dp"/>
into
<ImageView
style="#style/h2_image"/>
<style name="h2_image">
<item name="android:layout_width">80dp</item>
<item name="android:layout_height">47dp</item>
</style>
Very Simple.!
Just keep the cursor in the file from where you want to extract the style resource. Just a right click then click on refactor.
Refactor -> Extract -> style/layout.
Then give style a name and there you go..!
For more you can refer the documentation.
Try something like that:
<resources>
<style name="CustomImgView">
<item name="android:layout_width">80dp</item>
<item name="android:layout_height">47dp</item>
</style>
</resources>
<ImageView style="#style/CustomImgView" />
more information: https://developer.android.com/guide/topics/resources/style-resource.html
Try to Declare your Style like this :
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="YourCustomStyle">
<item name="android:layout_width">"80dp"</item>
<item name="android:layout_height">"47dp"</item>
<!-- another attributes -->
<item name="customAttr">value</item>
</style>
</resources>
And in your xml add this android:theme="#style/YourCustomStyle"
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`"
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/
I would like all the buttons of my android application use the same background image, that's all buttons have attribute:
android:background="#drawable/my_btn_bg"
Is there any way to define this in one place and take effect in the whole application's buttons? (instead of define the background on each button component)
I think define inside theme.xml could be a solution, but I do not know how to do it ? Could some one give me some hints?
For Android styles, you reference the preset attributes of R.attr. Here, you want to to reference android:buttonStyle. You can try this :
<style name="ApplicationStyle" parent="android:Theme">
<item name="android:buttonStyle">#style/yourButton</item>
</style>
Also look in this Themes and Styles
Define styles.xml with the below:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="buttonStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">#drawable/bg_button</item>
</style>
</resources>
And use the same style for any button within your application as:
<Button
android:text="Test Button"
android:id="#+id/btnTest"
style="#style/buttonStyle">
</Button>
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.