I'm using spinner and want to add spinner - to change behavior depends of states(focused, pressed)
sample project is here https://github.com/vovs/spinner_issue
My code:
activity_main.xml
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:spinnerMode="dropdown"
android:dropDownSelector="#drawable/spinner_state" />
spinner_state.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#color/black" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#color/red" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#color/red" />
<item
android:state_enabled="true"
android:drawable="#color/gray" />
</selector>
AndroidManifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
So, if I run app in emulator Android 4.0.2 API 14 and try to select some item or scroll using wheel of my mouse no any effect, that I set in selector(when press or scrolling - items should be red, but it is blue - default for ICS color).
For Android 2.2 API 8 when press or scroll using wheel(in this case state is focused) color is yellow[orange](default color for Android 2.2)
How to enable selector for spinner?
also an official bug...
https://code.google.com/p/android/issues/detail?id=24922
what helps:
<resources>
<style name="Theme.MyTheme" parent="#android:style/Theme.Holo.Light">
<item name="android:dropDownListViewStyle">#style/Theme.MyListView</item>
</style>
<style name="Theme.MyListView" parent="#android:style/Widget.Holo.Light.ListView">
<item name="android:listSelector">#drawable/orange_list</item>
</style>
</resources>
good luck!
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/lighter_gray_no_transparent" />
<item android:state_selected="true" android:drawable="#color/lighter_gray_no_transparent" />
<item android:state_pressed="true" android:drawable="#color/lighter_gray_no_transparent" />
<item android:drawable="#android:color/transparent"/>
</selector>
<color name="lighter_gray_no_transparent">#FFDDDDDD</color>
set backgroud selector for your item view, that's all. Work for me. The color alpha value should be FF, or it will show orange background, left one color value is #FFDDDDDD, the right one is #55DDDDDD
There may be other ways but here's what I understand from using one of the Generators.
Declare your own style for the spinner in your res/values/styles.xml pointing to your drawable.
<style name="myCustomSpinner" parent="android:Widget.Holo.Light.Spinner">
<item name="android:background">#drawable/spinner_state</item>
</style>
Create res/values/themes.xml and declare your own theme that inherits from the current theme. Within this theme, add an item for each attribute you're modifying and point it to your custom style from the last step. I think this could go in the styles file if you wanted, but since the generator separates them I follow suit.
<style name="myCustomTheme" parent="android:Theme.Light">
<item name="android:dropDownSpinnerStyle">#style/myCustomSpinner</item>
</style>
In your AndroidManifest, add android:theme="#style/myCustomTheme" to the opening application tag.
Your values for parent will depend on how the project is setup and I think this will style of the spinners in your project and not just one. Try it out and let me know what you get.
Related
Reference: How to specify background color in Color State List Resources?
I have followed the popular answer in the link above.
I have the following drawable;
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:drawable="#color/KeyActionPrimary"/> <!-- enabled -->
<item android:state_enabled="false"
android:drawable="#color/KeyActionButtonDeactivated"/> <!-- disabled -->
<item android:state_pressed="true"
android:drawable="#color/KeyActionPrimary"/> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#color/KeyActionPrimary"/> <!-- focused -->
<item android:drawable="#color/KeyActionPrimary"/> <!-- default -->
</selector>
I understand that it will always follow order of precedence and therefore in the current order only activated and deactivated likely to be triggered. Currently however I am not getting any results at all
My button;
<Button
android:id="#+id/buttonSTART"
android:layout_width="#dimen/btn_width"
android:layout_height="#dimen/btn_height"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="16dp"
android:background="#drawable/state_controlled_key_action_button"
android:padding="#dimen/btn_padding"
android:text="#string/btn_start"
android:textColor="#color/key_action_button_text_color"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/btnSCORES"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
The button comes out as white which in my material theme is the ColorPrimary attribute
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Home" parent="Theme.MaterialComponents">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/Primary</item>
<item name="colorPrimaryVariant">#color/PrimaryVariant</item>
<item name="colorSecondary">#color/Secondary</item>
<item name="colorSecondaryVariant">#color/SecondaryVariant</item>
<!--colorBackground appears behind scrollable content and is used for the default window-->
<!--background. colorSurface is mapped to the surface of components such as cards, sheets-->
<!--and menus. colorError is used to indicate an error state for components such as-->
<!--text fields.-->
<item name="android:colorBackground">#color/Background</item>
<item name="colorSurface">#color/Surface</item>
<item name="colorError">#color/Error</item>
<!--"On" colors define how text, icons and strokes are colored in relation to the surface-->
<!--on which they appear.-->
<item name="colorOnPrimary">#color/ColorOnPrimary</item>
<item name="colorOnSecondary">#color/ColorOnSecondary</item>
<item name="colorOnBackground">#color/ColorOnBackground</item>
<item name="colorOnSurface">#color/ColorOnSurface</item>
<item name="colorOnError">#color/ColorOnError</item>
<item name="android:fontFamily">#font/atma_medium</item>
<item name="android:textColor">#color/PrimaryTextColor</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">#color/StatusBar</item>
<!-- Customize your theme here. -->
<item name="actionBarSize">36dip</item>
</style>
</resources>
Firstly I am assuming that a theme covers the entire app but if you explicitly specify a background to a specific button it should override the theme value, please correct me if I am wrong.
I started this journey with a colorstatelist under res/colors. Read the link at the top which confirmed you need to use drawable. As I already had a color state list initially followed the advice of the answer which had 0 votes...
shape
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#color/primary_button_color" />
<stroke android:width="#dimen/btn_stroke" android:color="#color/primary_button_stroke_color"/>
<corners android:radius="#dimen/btn_radius"/>
</shape>
colorstatelist
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:color="#color/Primary"/> <!-- pressed -->
<item android:state_enabled="false"
android:color="#color/PrimaryButtonDeactivated"/> <!-- pressed -->
<item android:state_pressed="true"
android:color="#color/Primary"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#color/Primary"/> <!-- focused -->
<item android:color="#color/Primary"/> <!-- default -->
</selector>
Neither of these approaches is amending my button color. I am still always seeing a white button. I purposely chose horrid colors to ensure the change was obvious
What am I doing wrong? What else do you need to see?
UPDATE...........
Following discussion with #rcs I used my original colorstatelist from res/color with the backgroundtint property on the button and it works as intended
My final code:
For each button that requires a different style i have a button color or text color file in res/colors as per below
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/KeyActionColorOnPrimary"/>
<item android:state_enabled="true"
android:color="#color/KeyActionPrimary"/>
<item android:state_enabled="false"
android:color="#color/lime"/>
<item android:state_focused="true"
android:color="#color/KeyActionPrimary"/>
<item android:color="#color/KeyActionPrimary"/>
</selector>
Then within my button xml
android:backgroundTint="#color/key_action_button_color" // for button color
android:textColor="#color/key_action_button_text_color" // for text color
e.g.
<Button
android:id="#+id/buttonSTART"
android:layout_width="#dimen/btn_width"
android:layout_height="#dimen/btn_height"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:layout_marginBottom="16dp"
android:backgroundTint="#color/key_action_button_color"
android:padding="#dimen/btn_padding"
android:text="#string/btn_start"
android:textColor="#color/key_action_button_text_color"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/btnSCORES"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
This works with no additional styles. I can keep my material design theme without the button style being set to apt compat style. I am only using the above attributes in the layout xml for the button i want to have these styles
Since the first condition in your color_state is `android:state_enabled="true", which is always the case, thereby resulting in the same color. Get rid of it to notice the color changes
Refer the official docs for more info and try to use a color file resource instead of drawable as the docs says,
ColorStateLists are created from XML resource files defined in the "color" subdirectory directory of an application's resource directory
Edit:
As mentioned in this answer, applying the colorstate as a style to the Button tag should solve the issue
create a new style
<style name="AppTheme.CustomButtonStyle" parent="#style/Widget.AppCompat.Button.Colored">
<item name="android:backgroundTint">#color/state_controlled_key_action_button</item>
</style>
Activity
<Button
android:id="#+id/your_button_id"
style="#style/AppTheme.CustomButtonStyle"/>
Recently my ToggleButtons on an application I'm working on lost it's green light indicator. I started with the various tutorials and got together a set of 9 patch indicators to make them work correctly again. The only problem is the text now sits on top of the indicator, like so:
I would like the text on top like the old toggle buttons...is there any way to do this?
btn_toggle_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background" android:drawable="#android:drawable/btn_default" />
<item android:id="#+android:id/toggle" android:drawable="#drawable/btn_toggle" />
</layer-list>
my btn_toggle.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/btn_toggle_on" />
<item android:state_checked="false" android:drawable="#drawable/btn_toggle_off" />
</selector>
and the togglebutton tag in my activity:
<ToggleButton
android:background="#drawable/btn_toggle_bg"
style="#style/OurThemeName"
android:textOff="#string/fertilizer"
android:textOn="#string/fertilizer"
android:id="#+id/toggleFertilizer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false">
</ToggleButton>
<ToggleButton
android:background="#drawable/btn_toggle_bg"
style="#style/OurThemeName"
android:textOff="#string/irrigation"
android:textOn="#string/irrigation"
android:id="#+id/toggleIrrigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false">
</ToggleButton>
almost forgot the themes.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Overwrite the ToggleButton style -->
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">#drawable/btn_toggle_bg</item>
<item name="android:textOn">On</item>
<item name="android:textOff">Off</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
</style>
<style name="OurThemeName" parent="#android:Theme.Light">
<item name="android:buttonStyleToggle">#style/Widget.Button.Toggle</item>
<item name="android:textOn"></item>
<item name="android:textOff"></item>
</style>
</resources>
You need to set the Fill Area in your 9-Patch. You can add this areas including them in the right and bottom of your images using the Draw 9-patch tool. With this you are defining the area for your text label.
Check this tutorial explaining how 9-Patch works.
The "more" item will show on the ActionMode when there are more than 3 items, but it is automatically generated. I want change the background when I press it. What should I do?
I'm working on Android 4.0 API level 15.
I have solved this problem.
<style name="Theme.Seven" parent="android:Theme.Holo">
...
...
<item name="android:selectableItemBackground">#drawable/action_background</item>
</style>
action_background.xml as below:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime" >
<item android:state_pressed="true" android:drawable="#drawable/btn_dismiss_pressed" />
<item android:drawable="#android:color/transparent" />
</selector>
I've created custom ToggleButtons in Android and since all buttons inherit from the same xml I want to change how they act depending on state, so when the state is checked I want to change the shadow color but this does not seem to possible with the current SDK.
I've created an xml file which holds button_colors:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:color="#FFFFFF" />
<item
android:color="#000000" />
</selector>
But this only seems to work with text-color and not shadow color on the text.
Is there something I'm missing?
And rather not do this for every button manually in code since I want this to apply to every button in the app.
UPDATE EDIT:
My selector currently looks like this
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/button_gradient_selected" />
<item
android:drawable="#drawable/button_gradient" />
</selector>
But as I mentioned to the commentator below I can't seem to change the style/text-color-shadow from here since it only can take in a drawable it seems.
When I try to put in a different style on the button in here it force closes or either does not change the style depending on state. When I only try to put in the style here and have the drawable be set in the style it force closes. Either way it does not work it seems.
Seems that the Android framework does not support this.
From TextView.java:
case com.android.internal.R.styleable.TextView_textColor:
textColor = a.getColorStateList(attr);
break;
case com.android.internal.R.styleable.TextView_shadowColor:
shadowcolor = a.getInt(attr, 0);
break;
They treat textColor and shadowColor differently.
Please refer to my solution on a different StackOverFlow question. I extended TextView to give a working solution here. (Replace TextView with Button)
This is the Selector file you have to implement:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/btn_toggle_off" />
<item android:state_checked="true" android:drawable="#drawable/btn_toggle_on" />
</selector>
These are the picture tey used for the default ToggleButton:
btn_toggle_on and btn_toogle_off
You can have a selector for the shadow color like this: color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<item
android:state_pressed="true"
android:color="#color/btn_text_on" />
<item
android:state_focused="true"
android:color="#color/btn_text_on" />
<item
android:color="#color/btn_text_off" />
</selector>
and then use this selector while styling your button in styles.xml like this:
<style name="ButtonStyle">
<item name="android:textColor">#FF383C48</item>
<item name="android:textSize">12sp</item>
<item name="android:shadowColor">#drawable/color_selector</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">1</item>
<item name="android:typeface">sans</item>
<item name="android:textStyle">bold</item>
</style>
I am wanting to allow the user of my android application the ability to set some parameters. The radio button is ideal for this situation. However, I don't like the radio buttons are rendered.
Is it possible to change the radio button icon? For example, is it possible to create a custom layout for each row and in that layout reference my own icon and change the font et al.
Yes that's possible you have to define your own style for radio buttons, at res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:radioButtonStyle">#style/RadioButton</item>
</style>
<style name="RadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/radio</item>
</style>
</resources>
'radio' here should be a stateful drawable, radio.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="#drawable/radio_hover" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="#drawable/radio_normal" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="#drawable/radio_active" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="#drawable/radio_active" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/radio_hover" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/radio_normal_off" />
<item android:state_checked="false" android:drawable="#drawable/radio_normal" />
<item android:state_checked="true" android:drawable="#drawable/radio_hover" />
</selector>
Then just apply the Custom theme either to whole app or to activities of your choice.
For more info about themes and styles look at http://brainflush.wordpress.com/2009/03/15/understanding-android-themes-and-styles/ that is good guide.
You can put custom image in radiobutton like normal button.
for that create one XML file in drawable folder
e.g
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/sub_screens_aus_hl"
android:state_pressed="true"/>
<item android:drawable="#drawable/sub_screens_aus"
android:state_checked="true"/>
<item android:drawable="#drawable/sub_screens_aus"
android:state_focused="true" />
<item android:drawable="#drawable/sub_screens_aus_dis" />
</selector>
Here you can use 3 different images for radiobutton
and use this file to RadioButton like:
android:button="#drawable/aus"
android:layout_height="120dp"
android:layout_width="wrap_content"
The easier way to only change the radio button is simply set selector for drawable right
<RadioButton
...
android:button="#null"
android:checked="false"
android:drawableRight="#drawable/radio_button_selector" />
And the selector is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_checkbox_checked" android:state_checked="true" />
<item android:drawable="#drawable/ic_checkbox_unchecked" android:state_checked="false" /></selector>
That's all
yes....`
from Xml
android:button="#drawable/yourdrawable"
and from Java
myRadioButton.setButtonDrawable(resourceId or Drawable);
`
Here's probably a quick approach,
With two icons shown above, you shall have a RadioGroup something like this
change the RadioGroup's orientation to horizontal
for each RadioButton's Properties, try giving the icon for Button
under CompoundButton,
adjust the Padding and size,
and set the Background attribute when checked.
In case you want to do it programmatically,
checkBoxOrRadioButton.setButtonDrawable(null);
checkBoxOrRadioButton.setBackgroundResource(R.drawable.resource_name);