Allow styles to be themed - android

I have a simple problem. I'd like to provide multiple themes for my UI that a user can switch between
The problem is that I can use a theme that styles primitive UI classes, or I can use a style directly on an XML layout element, but I can't define a style that will then change based on the theme I have applied. Not all of the styling I want to do is based on the primitive type.
I would say that what I want to do is similar to using a CSS selector to style a class or ID, but themes only allow you to style an element name.
The best I can do right now is have a base style that inherits from EITHER of the actual styles I've built, as below:
My res/values/style.xml
// All styles have to be subclassed here to be made generic, and only one
// can be displayed at a time. Essentially, I'm doing the same thing as setting
// a theme does, but for styles. If I have 50 styles, I have to have 50 of
// these for each theme, and that's not DRY at all!
<style name="MyHeaderStyle" parent="MyHeaderStyle.Default"/>
<!--
<style name="MyHeaderStyle" parent="MyHeaderStyle.Textures"/>
-->
<style name="MyHeaderStyle.Default">
<item name="android:background">#android:color/background_dark</item>
</style>
<style name="MyHeaderStyle.Textures">
<item name="android:background">#drawable/header_texture</item>
</style>
Usage in my layout.xml:
<!-- Note that I can't use a theme here, because I only want to style a
SPECIFIC element -->
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="#style/MyHeaderStyle"
android:gravity="center">
</LinearLayout>

Try this method:
1) Define your own res/values/attrs.xml file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="AppTheme">
<attr name="myLinearLayoutStyle" format="reference" />
</declare-styleable>
</resources>
2) Assign the above attr to you LinearLayout
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?attr/myLinearLayoutStyle"
android:gravity="center">
</LinearLayout>
3) Assign a concrete style to this attr:
<style name="AppLightTheme" parent="android:Theme.Holo.Light">
<item name="#attr/myLinearLayoutStyle">#style/lightBackground</item>
</style>
<style name="AppTheme" parent="android:Theme.Holo">
<item name="#attr/myLinearLayoutStyle">#style/darkBackground</item>
</style>
<style name="lightBackground">
<item name="android:background">#D1CBF5</item>
</style>
<style name="darkBackground">
<item name="android:background">#351BE0</item>
</style>
Hope I understood correctly your question.

Related

How do I find all possibillities for Androids "Theme XML Style"?

The following file defines the base style used by my application:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Snapchat" parent="????" >
<item name="android:colorPrimaryDark">#color/Snapchat_violet</item>
<item name="android:colorPrimary">#color/Snapchat_blue</item>
<item name="android:colorAccent">#color/Snapchat_blue</item>
<item name="android:colorBackground">#color/Snapchat_white</item>
<item name="android:colorForeground">#color/Snapchat_yellow</item>
<item name="android:navigationBarColor">#color/Snapchat_grey</item>
</style>
</resources>
I want to use a standard theme as the parent to inherit from, but I can't find a listing of all standard themes and their corresponding xml names/paths.
I'm sorry if this is a newbish question, but I just can't find it in the android documentation.
check out following links it will help you
Theme.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
Style.xml
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/styles.xml
You can define multiple styles per file using tag but each style will have its name that uniquely identifies the style. Android style attributes are set using tag as shown below −
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomFontStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:capitalize">characters</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">12pt</item>
<item name="android:textColor">#00FF00</item>/>
The value for the can be a keyword string, a hex color, a reference to another resource type, or other value depending on the style property.
Using Styles
Once your style is defined, you can use it in your XML Layout file using style attribute as follows −
<TextView
android:id="#+id/text_id"
style="#style/CustomFontStyle"
android:text="#string/hello_world" />
</LinearLayout>
To implement a custom theme create or edit MyAndroidApp/res/values/themes.xml and add the following −
<resources>
...
<style name="MyCustomTheme" parent="android:style/Theme">
<item name="android:textColorPrimary">#ffff0000. </item>
</style>
...
</resources>
In your AndroidManifest.xml apply the theme to the activities you want to style −
<activity
android:name="com.myapp.MyActivity"
....
android:theme="#style/MyCustomTheme"
/>
Your new theme will be applied to your activity.
You can also have a look at http://www.tutorialspoint.com/android/android_styles_and_themes.htm
See style, stylable and attr.

ActionBar color change also ListView color

sometring strange is happening in my app, when I try to change color of ActionBar by
<style name="CustomAppTheme" parent="Theme.AppCompat.Light">
<item name="android:background">#ff00</item>
<item name="android:textColor">#DCDCDC</item>
</style>
is also changes color of my ListView and in other activity changes whole content below. Why is that? How to fix this?
ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/listviewbackground"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:longClickable="true"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
The documentation is pretty straight forward when it comes to overriding the default color & styles of the action bar. I'm assuming that you're probably making changes to your application's resource styles file at res/values/styles.xml. I'm also assuming that in your AndroidManifest.xml, you have the following property applied to the application tag:
android:theme="#style/AppTheme"
Again, from the documentation:
A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout.
So, when you change that styles.xml file, you're inadvertently changing the background color for all elements in your app.
Follow the instructions from the link 1, and you'll be good. Specifically,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/actionbar_background</item>
</style>
</resources>

Apply style only to specific TextViews

I have two themes that I switch back and forth between in my Android application. In some places, I have TextViews that I want to remain the default color. In other places, I want them to be another color (let's say orange). When changing themes, I'd like only the TextViews that were previously orange to become blue.
Is there a way to do this simply in Android? I had something in mind such as a class tag in html/css but can't seem to find anything.
EDIT: To clarify what I was referring to with the html/css equivalent:
<div>This text is black</div>
<div class="redDiv">This text is red</div>
.redDiv {
color:red;
}
Let's say you have two themes in your application: MyFirstTheme and MySecondTheme:
<style name="MyFirstTheme" parent="android:Theme">
<item name="android:textViewStyle">#style/MyFirstTheme.TextView</item>
[...]
</style>
<style name="MySecondTheme" parent="android:Theme">
<item name="android:textViewStyle">#style/MySecondTheme.TextView</item>
[...]
</style>
The textview styles defined in your styles.xml could look like:
<style name="MyFirstTheme.TextView" parent="#android:style/Widget.TextView">
<item name="android:textColor">#android:color/black</item>
[...]
</style>
<style name="MySecondTheme.TextView" parent="#android:style/Widget.TextView">
<item name="android:textColor">#color/orange</item>
[...]
</style>
<style name="PersistentTheme.TextView" parent="#style/MyFirstTheme.TextView">
<item name="android:textColor">#color/red</item>
</style>
So when you have a layout with two TextViews, you can have the one completely follow the active theme by not setting anything extra to it, and you can apply other appearance to your other TextView instance by specifying a style attribute for it:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- This textview will change text color when other style applied -->
<TextView
android:id="#+id/tv_styledependent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- This textview will always have the same (red) text color -->
<TextView
android:id="#+id/tv_persistent"
style="#style/PersistentTheme.TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_styledependent" />
</RelativeLayout>

How to reference a style in a custom theme

I have a login screen that is branded differently for different builds of my application. I need the background image to be different in the layout file for this screen, so I want to point to a different style for the top level container. I'm a bit at a loss at how to do this.
I have declared a styleable something like:
<resources>
<declare-styleable name="ThemeBase">
<attr name="loginPageContainerStyle" format="reference" />
</declare-styleable>
</resources>
I have several different themes for the application, as such:
<resources>
<style name="ThemeBase" parent="android:style/Theme.Light" />
<style name="ThemeOne" parent="ThemeBase">
<item name="loginPageContainerStyle">#style/loginPageContainerThemeOne</item>
</style>
<style name="ThemeTwo" parent="ThemeBase">
<item name="loginPageContainerStyle">#style/loginPageContainerThemeTwo</item>
</style>
</resources>
And I have defined the following styles:
<resources>
<style name="loginPageContainerThemeOne">
<item name="android:background">#drawable/background_theme_one</item>
</style>
<style name="loginPageContainerThemeTwo">
<item name="android:background">#drawable/background_theme_two</item>
</style>
</resources>
And finally a login.xml file something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/loginRoot"
style= [ ? WHAT GOES HERE ? ]
android:gravity="center_horizontal"
android:orientation="horizontal">
[ LAYOUT STUFF ... ]
</LinearLayout>
Am I doing anything wrong? Can this be done this way?
Ok I figured it out, the style reference should be:
style="?attr/loginPageContainerStyle"
Figured I would share.

Override all form element styles like HTML+CSS

In HTML/CSS you can override all paragraphs by defining p in CSS
I.e.
p { color: blue;
}
Can you do mimc this in Android? I'd like to define my set of styles for all form elements in styles.xml and then every layout I write would use these properties.
At the moment I have to specify the style in the layout: i.e
<EditText android:id="#+id/address1" style="#style/EditText" />
Which seems a bit redundant (and error prone).
Thanks,
John
You have to create an xml file which defines the style that you want, and store it in res/values folder.
<?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>
This is an example of an style, once you have your style. you can applay it to every view by doing something like this:
<TextView
style="#style/CodeFont"
android:text="#string/hello" />
This example is from:
http://developer.android.com/guide/topics/ui/themes.html

Categories

Resources