Android: how to set spinnerItemStyle, spinnerDropDownItemStyle to an individual Spinner in XML? - android

This code allows to set application-wide spinnerItemStyle and spinnerDropDownItemStyle.
<application
...
android:theme="#style/AppTheme" >
<style name="AppTheme" parent="#android:style/Theme">
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">#style/SpinnerItem.DropDownItem</item>
</style>
<style name="SpinnerItem" parent="#android:style/Widget.TextView.SpinnerItem">
<item name="android:textColor">#00FF00</item>
</style>
<style name="SpinnerItem.DropDownItem" parent="#android:style/Widget.DropDownItem.Spinner">
<item name="android:textColor">#FF0000</item>
</style>
But when I try to apply a style to just one Spinner in XML layout file, nothing happens:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/SpinnerColoredText"/>
<style name="SpinnerColoredText">
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
<item name="android:spinnerDropDownItemStyle">#style/SpinnerItem.DropDownItem</item>
</style>
How can I set spinnerItemStyle without changing application-wide Theme?

Related

Android Spinner Text Color dropdown

I cant figure out why my text color for the spinner does not change color. Could you help with figuring this out please.
<style name="Theme.Material_Dark." parent="Theme.AppCompat.Light.NoActionBar">
</style>
<style name="ActionBarThemeOverlay" parent="">
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
<item name="android:spinnerStyle">#style/SpinnerItem.DropDownItem</item>
</style>
<style name="SpinnerItem" parent="#android:style/Widget.TextView.SpinnerItem">
<item name="android:textColor">#color/actionbar_bg_dk</item>
</style>
<style name="SpinnerItem.DropDownItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
<item name="android:textColor">#color/White</item>
<item name="android:popupBackground">#424242</item>
</style>
Create a TextView layout.
dropdown_spinner_text.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinner_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:textColor="#color/colorBlack"
android:background="#color/colorWhite"/>
Then set that as your dropdown resource.
spinnerAdapter.setDropDownViewResource(R.layout.dropdown_spinner_text);
add the the declared item style to textAppearance
<style name="ActionBarThemeOverlay" parent="">
<item name="android:spinnerItemStyle">#style/SpinnerItem</item>
</style>
<style name="SpinnerItem" parent="#android:style/Widget.TextView.SpinnerItem">
<item name="android:textColor">#color/actionbar_bg_dk</item>
<item name="android:textAppearance">#style/SpinnerItem.DropDownItem"</item>
</style>
<style name="SpinnerItem.DropDownItem" parent="android:TextAppearance.Widget.TextView.SpinnerItem">
<item name="android:textColor">#color/White</item> //drop down item text color
<item name="android:popupBackground">#424242</item>
</style>

Several themes for view's custom styles

I have one app theme and many custom styles for different View.
For example, snippet of code:
<!-- styles.xml -->
<style name="AppBaseTheme" parent="#style/Theme.AppCompat.Light" />
<style name="title">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">22sp</item>
<item name="android:padding">10sp</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:textColor">#color/black</item>
<item name="android:background">#color/background_all_screen</item>
</style>
<style name="label">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#color/black</item>
<item name="android:textSize">18sp</item>
<item name="android:layout_alignParentLeft">true</item>
<item name="android:layout_marginLeft">5dp</item>
</style>
<style name="button">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">2dp</item>
<item name="android:textSize">18sp</item>
<item name="android:textColor">#color/white</item>
<item name="android:background">#color/blue</item>
</style>
And now I want make many color themes for application. This is mean, that in different color themes custom View was in different colors.
For example, in one color theme button is blue, in another - red.
How can I implement this resources for easy change of theme?
There is a useful tutorial, but what about the custom styles for elements?
UPDATE:
I try it, but this is nor work:
<style name="Button.MyButton" parent="android:style/Widget.Button">
<item name="android:background">#drawable/shape</item>
</style>
<style name ="Button.MyButton.Theme1">
<item name="android:textColor">#000000</item>
</style>
<style name ="Button.MyButton.Theme2">
<item name="android:textColor">#FFFFFF</item>
</style>
<Button
android:id="#+id/save_button"
android:layout_width="0px"
style="#style/Button.MyButton"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/save"/>
This is answer. I just use attributes and after that I can use different colors in different themes.
<!-- values/attributes.xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="myCoolColor" format="color" />
</resources>
<!-- styles.xml, define myCoolColor for each theme -->
<style name="Theme.MyApp" parent="#style/Theme.Light">
<item name="myCoolColor">#123456</item>
</style>
<style name="Theme.MyApp.Dark" parent="#style/Theme.Dark">
<item name="myCoolColor">#654321</item>
</style>

Using A Custom Attribute That Is In A Style With A View

Lets say I have a custom attribute in a few styles like so:
<style name="Theme1" parent="android:Theme.Holo">
<item name="textViewBackground">#000022</item>
<item name="android:background">#000000</item>
</style>
<style name="Theme2" parent="android:Theme.Holo">
<item name="textViewBackground">#AA2200</item>
<item name="android:background">#000000</item>
</style>
Can I then reference that in a standard view like a TextView? Something like:
<TextView
android:id="#+id/txtNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#CurrentTheme.textViewBackground"
android:text="Number" />
You need to define a TextView style, like this:
<style name="Theme1" parent="android:Theme.Holo">
<item name="android:textViewStyle">#style/TextViewStyle1</item>
<item name="android:background">#000000</item>
</style>
<style name="Theme2" parent="android:Theme.Holo">
<item name="android:textViewStyle">#style/TextViewStyle2</item>
<item name="android:background">#000000</item>
</style>
<style name="TextViewStyle1" parent="#android:style/Widget.TextView">
<item name="android:background">#000022</item>
</style>
<style name="TextViewStyle2" parent="#android:style/Widget.TextView">
<item name="android:background">#AA2200</item>
</style>
Then you won't even need to set the android:background attribute on each TextView in your xml, it will apply to all TextViews that do not specify a different background.

ActionBar Custom Title Color

<?xml version="1.0" encoding="utf-8"?>
<style name="OmlActionBarTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/OmlActionBar</item>
<item name="android:homeAsUpIndicator">#drawable/back_arrow</item>
<item name="android:titleTextStyle">#style/OmlActionBarTitle</item>
<item name="android:subtitleTextStyle">#style/OmlActionBarSubTitle</item>
</style>
<style name="OmlActionBar" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/action_bar_white</item>
</style>
<style name="OmlActionBarTitle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#color/action_bar_blue</item>
</style>
<style name="OmlActionBarSubTitle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Subtitle">
<item name="android:textColor">#color/action_bar_blue</item>
</style>
Hi,I need to customize my actionBar title Text Color and subTitle Text Color. I used the above style but there is no change.
Try The Following...
style.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="android:windowBackground">#drawable/login_background</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#color/theme_blue</item>
<item name="titleTextStyle">#style/ActionBarTitleText</item>
<item name="subtitleTextStyle">#style/ActionBarSubTitleText</item>
</style>
<style name="ActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/stdDarkBlueText</item>
<item name="android:textSize">12sp</item>
</style>
<style name="ActionBarSubTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle">
<item name="android:textColor">#color/stdDarkBlueText</item>
<item name="android:textSize">12sp</item>
</style>
</resources>
Apply this theme to your application in Manifest File..
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#drawable/ic_icon128"
android:label="#string/app_name"
android:theme="#style/AppTheme" // apply theme here
android:windowSoftInputMode="stateHidden" >
i have done with simple one line code
actionBar.setTitle(Html.fromHtml("<font color='#ff0000'>ActionBartitle </font>"));

Android: Title Bar Color don't change

I wanna change my Apps Titlebar color and tried it in this way:
Part of the manifest file:
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/Theme.MyTitleBar" ></application>
The style block part of the file styles.xml:
<style name="Theme.MyTitleBar" parent="android:Theme">
<item name="android:colorBackground">#FFFFFF</item>
<item name="android:background">#FFFFFF</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16sp</item>
</style>
But only the text color and attributes changes correct. Any guest what I did wrong?
EDIT: I added the part for the ActionBar (sorry I put only for Dialogs at first)
You can follow this, it will allow you to change moreover other attributes that just the title bar:
Just call your custom theme in the manifest file:
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
Then create these 2 styles in your styles.xml. The first one defines the theme, the second one, one of the styles to apply on the theme:
<style name="AppTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:actionBarStyle">#style/ActionBarStyle</item>
</style>
<style name="ActionBarStyle" parent="android:style/Widget.Holo.Light.ActionBar">
<item name="android:colorBackground">#FFFFFF</item>
<item name="android:background">#FFFFFF</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">16sp</item>
</style>
NOTE: In this example, I customize the theme Holo Light as you can see with this: android:style/Widget.Holo.Light.ActionBar.
Add This style.xml File in your project according to your requirment...
<item name="android:actionBarStyle">#style/myActionBar</item>
</style>
<style name="myActionBar" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:titleTextStyle">#style/myActionBar.titleTextStyle</item>
</style>
<style name="myActionBar.titleTextStyle" parent="#android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse">
<item name="android:textColor">#color/darkgrey</item>
</style>
<style name="myActionBar" parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#e6ebc3</item>
</style>

Categories

Resources