I have a quick question.
Is there a way, using eclipse, to define a default theme for my app, or do I have to configure each activity and asset to match my desired theme?
If there is a way, how do I go about doing that?
Define your android:theme in application element of your manifest.
Create an xml file in res\values. In it create a default layout style like this (you can define other stuff also this is just an example):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
Use one style for each default theme you want to create. Then in the Widget you want to use this style you enter like this (again just an example):
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
This TextView will now inherit the settings declared in the style.
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"
How can I customize all widgets in the application by creating one style and apply it to application through android:theme in AndroidManifest?
Here's an example that might help (it might not work verbatim as I've tweaked it to simplify it, and to showcase some of the other things you can do).
In res\values\styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TextColorForTheme">
<item name="android:textColor">#color/red</item>
</style>
</resources>
In res\values\themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="myTheme" parent="#style/android:Theme">
<item name="android:listSeparatorTextViewStyle">#style/TextColorForTheme</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowFrame">#null</item>
</style>
</resources>
Then in your AndroidManifest.xml, set either the whole application, or individual Activities to use that theme:
<application
android:theme="#style/myTheme"
<snip>
<activity
android:theme="#style/myTheme"
<snip>
Alternatively, you can set the theme in the code for your Java Activity:
#Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setTheme(R.style.myTheme);
<snip>
You won't be able to do this by creating just one style. A theme is in essence a meta-style that defines the default styles for each of the different widgets available. You will start by creating a theme (which is itself a style) with a parent of one of the existing system themes and setting the default style attributes for each widget you wish to change from the base theme. For example, if you had a different button style that you wanted to set as default in your theme, you might add the following to your theme definition:
<item name="android:buttonStyle">#style/MyButtonStyle</item>
See http://developer.android.com/guide/topics/ui/themes.html for more info.
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>
I'm trying to learn something about styles and themes for Android. I've found that, if I add the android:theme attribute in the manifest for Application or Activity, style is applied to the entire app/activity. I think it would be useful to be able to define a style that is applied for example only to buttons. Do you know if there is a way to apply a certain style only for a certain kind on Views, as in CSS?
Here's a general example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomButton" parent="#android:style/Widget.Button">
<item name="android:textColor">#FF0000</item>
</style>
<style name="CustomTheme" parent="android:Theme">
<item name="android:buttonStyle>#style/CustomButton</item>
</style>
</resources>
It's easy to do, just read the official reference:
http://developer.android.com/guide/topics/ui/themes.html
EDIT
Obviously you'll need to specify the style you create on each button/view you want it to be applied (like in css, for the class attribute)
I'm using Theme.Dialog on one of my activities. I'm using setTitle(mMyTitle) to set the title. I would also like to set the textAppearance. How would I go about this?
I'm not sure if I can make my own title layout for this, since I'm already using Theme.Dialog.
Create a new style and extend Theme.Dialog then override what you want to change. Example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyActivityDialogTheme" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/MyTitleStyle</item>
</style>
<style name="MyTitleStyle" parent="#android:style/DialogWindowTitle">
<item name="android:textAppearance">#style/MyTextApperance</item>
</style>
<style name="MyTextApperance" parent="#android:style/TextAppearance.DialogWindowTitle">
<item name="android:textColor">#ffff0000</item>
<item name="android:textSize">36sp</item>
</style>
</resources>
Anything you don't specify will use the Dialog defaults. I've only specified the colour and font size of the textApperance in this example.
Now just use MyActivityDialogTheme (or whatever you call your style) as the theme for your activity instead of Theme.Dialog.
Check this out first, but I believe what you're looking for is about 3/4 the way down when it discusses tweaking a built-in resource by referencing it as a parent for your custom theme. As such:
<color name="custom_theme_color">#b0b0ff</color>
<style name="CustomTheme" parent="android:Theme.Dialog">
<item name="android:windowBackground">#color/custom_theme_color</item>
<item name="android:colorBackground">#color/custom_theme_color</item>
</style>
Hope it's what you're looking for.