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.
Related
I have trouble in changing the background color and text color of my buttons in my app.
I made a new theme to play around and try it out.
My manifest looks like this:
<application
...
android:theme="#style/OrangeWhite">
<activity android:name=".MainActivity">
...
</application>
My theme.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="OrangeWhite" parent="android:Theme.Light">
<item name="android:windowBackground">#color/OrangeWhite_bg_color</item>
<item name="android:textColor">#color/OrangeWhite_wh_text</item>
<item name="android:colorButtonNormal">#color/OrangeWhite_bg_btn</item>
</style>
</resources>
Changing text or background color of an activity works just fine. The problem is the button. Do I need to specify on every button to use the theme OrangeWhite?...
If you are using AppCompat theme as parent than try below code
If you need to change the style of a specific button, you can define a new style, inheriting one of the parent styles described above. In the example below I just changed the background and font colors:
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#color/Red</item>
<item name="android:colorButtonNormal">#color/Red</item>
<item name="android:textColor">#color/White</item>
</style>
Then you just need to apply this new style on the button with:
android:theme="#style/AppTheme.Button"
I have something like this in my library project. I want to use that custom defined theme in my concrete project but Im not able to reference it.How can I achieve this?
<?xml version="1.0" encoding="utf-8"?>
<style name="MenuDialog" parent="android:Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
<item name="android:windowAnimationStyle">#null</item>
</style>
What I did for workaround for now is: Im setting the theme not in the manifest file instead of that Im setting it before setContentView method.
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(package_library.R.style.MenuDialog);
}
Hope you are looking for this, if I understand your question correctly
Use this:
Create theme in style.xml file
<style name="Theme.SettingsBackground" parent="#android:style/Theme.NoTitleBar">
<item name="android:windowBackground">#android:color/black</item>
</style>
and then in manifest file use:
<activity android:name=".Settings" android:theme="#style/Theme.SettingsBackground"></activity>
Do this for all sub activities which you want.
For example when I define a style tag in XML, all views of all types get that theme. Is there any solution to difference between styles for view types? here is some code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="buttonTheme" parent="android:Theme.Holo">
<item name="android:drawableLeft">#drawable/ic_launcher</item>
</style>
<style name="textTheme" parent="android:Theme.Holo">
<item name="android:textColor">#ff0000</item>
</style>
</resources>
I think there's a small confusion in terms between a style and a theme. You are talking about defining custom styles for a widget in your application. A Theme is a collection of these styles applied globally to an Activity or Application. The custom styles you have created for a button and text view can be applied to a new custom theme so all buttons and text items share the same attributes.
I think what you are looking for is something more like this.
<style name="ApplicationTheme" parent="android:Theme.Holo">
<item name="buttonStyle">#style/MyButtonStyle</item>
<item name="textAppearance">#style/MyTextAppearance</item>
</style>
<style name="MyButtonStyle" parent="android:Widget.Button">
<item name="android:drawableLeft">#drawable/ic_launcher</item>
</style>
<style name="MyTextAppearance" parent="android:TextAppearance">
<item name="android:textColor">#ff0000</item>
</style>
Here, we have created two new styles (one for the button appearance and one for default text appearance), and then applied those as attributes in a new custom theme. You can now apply this theme in your manifest or in View constructors by referencing #style/ApplicationTheme or R.style.ApplicationTheme
You can do this by adding the attribute android:theme="" to the specific activity in your AndroidManifest.xml file
Example:
<activity android:theme="#style/CustomTheme">
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.
I have an android manifest with an activity that I want to apply to styles to:
<activity android:label="#string/app_name" android:name="Language" android:theme="#android:style/Theme.NoTitleBar>
Is how it looks right now, but while keeping the NoTitleBar attribute, I would like to add this attribute as well:
android:style/Theme.Light"
But I'm just so new to Android that I can't figure it out.
Please help!
You cannot have more than one theme applied at once in your manifest.
I believe there is a theme Theme.Light.NoTitleBar that will do what you want - but I will show you below how you can easily do this yourself and customize more.
What you need to do is create a theme which has either Theme.NoTitleBar or Theme.Light as it's parent and customizes the bits you want -- in this case the easiest way is to create a theme with Theme.Light as it's parent and just hide the title bar (rather than have the Theme.NoTitleBar as the parent and then have to make everything light which is much harder!).
You can do this with the following code in your themes.xml file in the values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- use the Android Light theme as the base for our own theme -->
<style name="MySuperTheme" parent="#android:style/Theme.Light">
<!-- hide the Window Title -->
<item name="android:windowNoTitle">true</item>
<!-- You could change the scrollbar, checkbox style, anything! -->
</style>
</resources>
Then use android:theme="#style/MySuperTheme" for your activity (or you could even apply it to your whole application by placing it on the application element -- if you apply a style to an individual activity and have one set for the whole application as well then the style of the individual activity will be the one shown).
Take a look at the Android themes.xml for a list of all the things you can customize in your own theme.
You can also look at all of the Android styles to see how they are done.
You'll need at least 2 styles, best inheriting from base styles, e.g. Theme.Material variants, or if you use appcompat then Theme.AppCompat variants. In each style override values such as colours, drawables etc with theme-specific values.
values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- original theme attributes -->
...
<item name="android:textColorPrimary">#FFFFFF</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<!-- alternative theme attributes -->
...
<item name="android:textColorPrimary">#000000</item>
</style>
This will be sufficient if you only use framework or appcompat attributes (e.g. colorAccent, android:textColorPrimary etc) in your layouts. But if you need your own attributes (e.g. a drawable with color that is different per theme), then you will need to define custom attributes.
values/attrs.xml
<attr name="themedMenuStoryDrawable" format="reference" />
<attr name="themedMenuCommentDrawable" format="reference" />
...
Specify theme-specific values for your custom attributes:
values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- original theme attributes -->
...
<item name="themedMenuStoryDrawable">#drawable/ic_subject_white_24dp</item>
<item name="themedMenuCommentDrawable">#drawable/ic_mode_comment_white_24dp</item>
</style>
<style name="AppTheme.Dark" parent="Theme.AppCompat">
<!-- alternative theme attributes -->
...
<item name="themedMenuStoryDrawable">#drawable/ic_subject_black_24dp</item>
<item name="themedMenuCommentDrawable">#drawable/ic_mode_comment_black_24dp</item>
</style>
Then refer to your custom attributes with ?attr/ prefix in layouts, menus etc:
menu/my_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#id/menu_comment"
android:icon="?attr/themedMenuCommentDrawable" />
<item android:id="#id/menu_story"
android:icon="?attr/themedMenuStoryDrawable" />
</menu>
Check out my blog post for the complete guide.