Switching Styles - android

Let's say I have multiple styles.xml files (with different names of course) for themes. Is it possible to choose which file the app should pull from?
White style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="mybutton">
<item name="android:layout_width">45dp</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:textColor">#202020</item>
<item name="android:textSize">20dp</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#drawable/white_btnbg</item>
</style>
...
Black style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="mybutton">
<item name="android:layout_width">45dp</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">20dp</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#drawable/black_btnbg</item>
</style>
...
So then, in my layout xml all my button's styles would be set to "mybutton" and depending which theme the user chose it would pull from the coordinating file.
Since it isn't possible to change a view's style at runtime is it possible to do this? Or is there a better way (i'm sure there is) to change styles?

Use multiple styles with different names. Then you can set the backGround programmatically:
Button b = new Button(this);
b.setBackgroundResource(R.drawable.blabla);

Related

Is there a way to style PreferenceFragmentCompat

The title says it all: how can I style my PreferenceFragmentCompat. My v14/style.xml is
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:editTextStyle">#style/Widget.EditText.White</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay</item>
</style>
<style name="Widget.EditText.White" parent="#android:style/Widget.EditText">
<item name="android:textColor">#ffffffff</item>
</style>
</resources>
The base theme has a black background - the preferences screen is then unreadable as I have black text on that black background.
I've tried many things but I cannot change the text colour.
What I've had to do is to set the fragment container background colour to white whilst the settings fragment is active. An ugly hack and not what I want to have to do.
To answer my own question: my v14/style.xml is now
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:editTextStyle">#style/Widget.EditText.White</item>
<item name="preferenceTheme">#style/PreferenceThemeOverlay.v14.Material</item>
</style>
<style name="Widget.EditText.White" parent="#android:style/Widget.EditText">
<item name="android:textColor">#ffffffff</item>
</style>
<style name="PreferenceThemeOverlay.v14">
<item name="android:background">#color/settings_background</item>
<item name="android:textColor">#color/settings_text_colour</item>
<item name="android:textColorSecondary">#color/settings_text_colour_secondary</item>
</style>
</resources>
My problem is now to style a ListPreference.
Why is this so hard? If there is any documentation relating to styling preferences, it is well hidden. Grrrrrr!
Take a look at README for github project
Gericop/Android-Support-Preference-V7-Fix

Text Select Handle doesn't seem to change

I have this theme in values/styles.xml, that I apply to my whole application.
<!-- Base application theme. -->
<style name="AppTheme" parent="#style/_AppTheme">
<!-- a bunch of things... -->
<item name="android:textColorHighlight">#9999cc00</item>
<!-- a bunch of things... -->
</style>
<style name="_AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"/>
Then in values-v9
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="#style/_AppTheme">
<item name="android:textSelectHandleLeft">#drawable/apptheme_text_select_handle_left</item>
<item name="android:textSelectHandleRight">#drawable/apptheme_text_select_handle_right</item>
<item name="android:textSelectHandle">#drawable/apptheme_text_select_handle_middle</item>
</style>
</resources>
And finally in values-v11
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="#style/_AppTheme">
<item name="android:dropDownSpinnerStyle">#style/SpinnerAppTheme</item>
<item name="android:listChoiceBackgroundIndicator">#drawable/apptheme_list_selector_holo_light</item>
<item name="android:activatedBackgroundIndicator">#drawable/apptheme_activated_background_holo_light</item>
<item name="android:textColorHighlight">#9999cc00</item>
<item name="android:textSelectHandleLeft">#drawable/apptheme_text_select_handle_left</item>
<item name="android:textSelectHandleRight">#drawable/apptheme_text_select_handle_right</item>
<item name="android:textSelectHandle">#drawable/apptheme_text_select_handle_middle</item>
</style>
</resources>
All of the drawables are in their respective folder.
This has no effect in changing the text select handle in any part of the application.
Is this because of the AppCompat library use maybe?
How do I repair?
Using this link
http://android-holo-colors.com/
The worst part here was to find the "name" for this item and how it is called inside the theme. So I looked through every drawable in the android SDK folder and finally found the drawables named "text_select_handle_middle", "text_select_handle_left" and "text_select_handle_right".
So the solution is simple: Add these drawables with customized design/color to your drawable folder and add them to your theme style definition like:
<style name="MyCustomTheme" parent="#style/MyNotSoCustomTheme">
<item name="android:textSelectHandle">#drawable/text_select_handle_middle</item>
<item name="android:textSelectHandleLeft">#drawable/text_select_handle_left</item>
<item name="android:textSelectHandleRight">#drawable/text_select_handle_right</item>
</style>

Give text of selected tab a different color using ViewPagerIndicator

I've tried to let the SampleTabsStyled demo from the ViewPagerIndicator change the color of the text of the currently selected tab without success.
However, the SampleTitlesStyledTheme demo does change its text color when switching between titles/tabs.
When looking inside the styles xml:
<resources>
...
<style name="CustomTitlePageIndicator">
<item name="android:background">#18FF0000</item>
<item name="footerColor">#FFAA2222</item>
<item name="footerLineHeight">1dp</item>
<item name="footerIndicatorHeight">3dp</item>
<item name="footerIndicatorStyle">underline</item>
<item name="android:textColor">#AA000000</item>
<item name="selectedColor">#FF000000</item>
<item name="selectedBold">true</item>
</style>
...
<style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">
<item name="android:background">#drawable/custom_tab_indicator</item>
<item name="android:textAppearance">#style/CustomTabPageIndicator.Text</item>
<item name="android:textColor">#FF555555</item>
<item name="android:textSize">16sp</item>
<item name="android:divider">#drawable/custom_tab_indicator_divider</item>
<item name="android:dividerPadding">10dp</item>
<item name="android:showDividers">middle</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">8dp</item>
<item name="android:fadingEdge">horizontal</item>
<item name="android:fadingEdgeLength">8dp</item>
</style>
<style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
<item name="android:typeface">monospace</item>
</style>
...
</resources>
I see that the SampleTitlesStyledTheme demo uses the CustomTitlePageIndicator style, which defines a selectedColor item. So (perhaps naively) I thought to add
<item name="selectedColor">#FF000000</item>
to the style the SampleTabsStyled demo is using, CustomTabPageIndicator, but, alas, that didn't work.
The question seems obvious, but I'll ask anyway: is there a way (using the present styles xml) to let the currently selected text of a tab in the SampleTabsStyled demo have a different color than the other tabs? If so, how?
EDIT
Oh, and I'm using this in combination with ActionBarSherlock, in case that is important...
Assuming you don't mind creating an extra xml, first try include the attribute android:textColor in your CustomTabPageIndicator.Text (or CustomTabPageIndicator) like this:
<style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
<item name="android:textColor">#drawable/tab_text_color</item>
...
</style>
where tab_text_color.xml is put under res/drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:color="#color/text_tab_selected" />
<item
android:state_selected="false"
android:color="#color/text_tab_unselected" />
</selector>
Finally, just define the two colors #color/text_tab_selected and #color/text_tab_unselected in your color resource file colors.xml located in res/values folder (create one if it does not exist already). For example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="text_tab_selected">#FF000000</color>
<color name="text_tab_unselected">#FF555555</color>
</resources>
Try this
Simplest way
<android.support.design.widget.TabLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="#color/White"
app:tabSelectedTextColor="#color/Blue"
app:tabIndicatorColor="#color/Yellow"
app:tabMode="fixed"
app:tabGravity="fill"/>
Don't forgot to add this Dependency
compile 'com.android.support:design:23.1.1'

Textview: using the dialog title style: #android:style/TextAppearance.DialogWindowTitle

I am currently trying tu build my own DialogFragment theme.
One of my requirement is to use an icon at the right of the title.
First idea
Simply using:
this.getDialog().getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dialog_title);
But unfortunately, this line of code has no result.
Second idea
Provinding my own layout -this.setContentView()- with this textview:
<TextView
android:id="#+id/tvTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="From XML"
android:textAppearance="#android:style/TextAppearance.DialogWindowTitle" />
This works, because I can read the text, but it still written in default black font.
I was expecting TextAppearance.DialogWindowTitle to give my text a title appareance (blue in Holo with big font size)
Any idea or suggestion?
EDIT
This nearly did the trick:
<style name="Dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/MyOwnDialogTitle</item>
</style>
<style name="MyOwnDialogTitle">
<item name="android:drawableRight">#drawable/MyRightImage</item>
</style>
but android:drawableRight just broke the title layout:
SECOND EDIT
This is a little bit better:
<style name="Dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/MyOwnDialogTitle</item>
</style>
<style name="MyOwnDialogTitle">
<item name="android:textAppearance">#android:style/TextAppearance.DialogWindowTitle</item>
<item name="android:drawableRight">#drawable/icon</item>
</style>
Maybe you can extend the default style in this manner:
res/values/dialogstyles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Dialog" parent="#android:style/Theme.Dialog">
<item name="android:windowTitleStyle">#style/MyOwnDialogTitle</item>
</style>
<style name="MyOwnDialogTitle">
<item name="android:drawableRight">#drawable/MyRightImage</item>
</style>
</resources>
res/values-v11/dialogstyles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Dialog" parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowTitleStyle">#style/MyOwnDialogTitle</item>
</style>
</resources>
And override your DialogFragment's onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.Dialog);
}
Working Example
I am searching for your first requirement. but for second requirement, why dont you try with changing the color to style of the TextAppearance.DialogWindowTitle with your custom style ?
I have just check the android resources where there is style like below:
<style name="DialogWindowTitle">
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:textAppearance">#style/TextAppearance.DialogWindowTitle</item>
</style>
and for TextAppearance.DialogWindowTitle style:
<style name="TextAppearance.DialogWindowTitle">
<item name="android:textSize">18sp</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">?textColorPrimary</item>
</style>
Now, you can change the color of your TextAppearance.DialogWindowTitle style and do as you want.
Hope you got the point.
Feel free to comment.
Have you tried to use #android:style/Theme.Holo.Light.Dialog as your parent in styles? This will work only in Android 3+ because in earlier version there is no theme Holo, but you can create your own to look like it for lower versions of Android.

How to reference from drawable to style

My app with tabs has two themes. In each theme tabs have different images in selected and unselected state. How I can properly reference to image by theme?
For example. I have in themes.xml
<?xml version="1.0" encoding="utf-8"?>
<style name="LightTheme" parent="#android:style/Theme.Light">
<item name="tabShows">#drawable/ic_tab_shows_unselected_light</item>
<item name="tabShowsSelected">#drawable/ic_tab_shows_selected_light</item>
<item name="tabNews">#drawable/ic_tab_news_selected_light</item>
<item name="tabNewsSelected">#drawable/ic_tab_news_unselected_light</item>
</style>
<style name="DarkTheme" parent="#android:style/Theme.Black">
<item name="tabShows">#drawable/ic_tab_shows_unselected_dark</item>
<item name="tabShowsSelected">#drawable/ic_tab_shows_selected_dark</item>
<item name="tabNews">#drawable/ic_tab_news_selected_dark</item>
<item name="tabNewsSelected">#drawable/ic_tab_news_unselected_dark</item>
</style>
Also I have a tab_shows.xml and tab_news.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/ic_tab_shows_selected_light"/>
<item android:state_selected="false" android:drawable="#drawable/ic_tab_shows_unselected_light" />
How I can reference to needed image in selector according to current theme?
This not work for me
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="?tabShowsSelected"/>
<item android:state_selected="false" android:drawable="?tabShows" />
In layout files this works, I mean reference to style via ?styleName
Build your style A and style B
in your case you put android:drawable="#drawable/ic_tab_shows_selected_light" instead of background (I just copied snipets from my code)
#000
<style name="styleB">
<item name="android:background">#000</item>
</style>
your theme A
<style name="Theme.A">
<item name="pageBackground">#style/styleA</item>
</style>
theme B
<style name="Theme.Blue">
<item name="pageBackground">#style/styleB</item>
</style>
in your attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="pageBackground" format="reference" />
</resources>
finally in your widget you do style="?pageBackground"
You can find your answer here
http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html
Edit
(Additional information by Lukap in comments)
Define one or more themes in themes.xml and set the definitions of your styles there.
Define custom attributes, a.k.a. custom styles, in attrs.xml.
Describe what the values of your custom styles are in styles.xml.
But you will need read more about the attrs.xml
<item name="android:background">? android:attr/activatedBackgroundIndicator</item>
</style>
Instead, we are referring to the value of some other attribute – activatedBackgroundIndicator – from our inherited theme. Whatever the theme defines as being the activatedBackgroundIndicator is what our background should be.

Categories

Resources