Spinner background is not correct on Android 6.0+ - android

I use Android project(old project) on Eclipse.
I install ADT from here:
https://dl-ssl.google.com/android/eclipse/
In my manifest.xml
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
snippet of xml layout:
<Spinner
android:id="#+id/currencySpinner"
style="#style/racommon_spinner_custom_style"
android:layout_width="0dip"
android:layout_height="40dip"
android:layout_weight="0.3"
android:prompt="#string/currency" />
here styles.xml
<style name="racommon_spinner_custom_style" parent="#android:style/Widget.Holo.Light.Spinner">
<item name="android:background">#drawable/racommon_spinner_custom_bg</item>
<item name="android:clickable">true</item>
</style>
Here drawable racommon_spinner_custom_bg:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/spinner_selected_custom_border"/>
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/spinner_pressed_custom"/>
<item android:drawable="#drawable/spinner_default_custom" />
</selector>
here res\drawable-mdpi\spinner_pressed_custom.9.png
Here result on Android 4.3
and here result on Android 6.0+
Here when click on spinner:
As you can see the background of spinner is different than on Android 4.0. On android 4.0. is correct spinner's background. But in android 6.0+ not correct.
Here result nine-pathch generator
Why?

I think its a problem of dpi, so i would suggest you make 9 patch image according to dpi
go to Below Link
Click Here
and Drag your current 9 patch image here, it will generate the images according to dpi.

Since you aren't setting gradient background programmatically, the problem is just with the style used from deprecated theme: Widget.Holo.Light.Spinner.
Just replace it with up to date theme like Widget.AppCompat.Light.Spinner.
Also you can try just to update targetSdkVersion and use more recent SDK version if that's an option for you.
Some clarification:
the actual cause of the gradients to be broken on newer Android versions is the new requirement to specify values in dp, see Android Gradient on Lollipop not working. - API 21

Related

android:windowBackground not working above android 12

I have a 3 devices, they run from android 10,11 and 12. But the device on android 12 seems to have an issue with windowBackground. I have used the windowBackground as a splashscreen/loadingscreen. This appears to work fine on android 10 and 11 but on android 12 it seems to pick a different image, which I think is my applogo image.
This is the splash_image
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/papaya_orange"/>
<item android:drawable="#drawable/logo_transparant_white_loading_screen"
android:width="250dp"
android:height="95dp"
android:gravity="center" />
</layer-list>
In my styles.xml I have made this:
<style name="LoadingScreen" parent="DarkMode">
<item name="android:windowBackground">#drawable/splash_image</item>
<item name="android:statusBarColor">#color/papaya_orange</item>
</style>
In the manifest I have set the theme to android:theme="#style/LoadingScreen" >
And in my mainactivity I already declared that It switches themes, so nothing wrong there.
So this should work fine, but for some odd reason it doesn't work on android 12 like how it works on 10 and 11. Any guesses on why this doesn't work?
The problem is Android 12 migrations for Splash Screen. Check migration official documentation.

android - Set Switch Off color [duplicate]

The switch widget introduced in API 14 is styled by default with holo theme.
I want to style it slightly different, changing its colors and shape a bit for branding reasons. How does one go about this? I know it must be possible, as ive seen the difference between default ICS and Samsung's touchwiz theme
I assume I'll need some state drawables, and I've seen a few styles in http://developer.android.com/reference/android/R.styleable.html with Switch_thumb and Switch_track that look like what I might be after. I just don't know how to go about using them.
I'm using ActionbarSherlock if that makes a difference. Only devices running API v14 or above will be able to use a switch at all, of course.
You can define the drawables that are used for the background, and the switcher part like this:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="#drawable/switch_thumb"
android:track="#drawable/switch_bg" />
Now you need to create a selector that defines the different states for the switcher drawable.
Here the copies from the Android sources:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/switch_thumb_disabled_holo_light" />
<item android:state_pressed="true" android:drawable="#drawable/switch_thumb_pressed_holo_light" />
<item android:state_checked="true" android:drawable="#drawable/switch_thumb_activated_holo_light" />
<item android:drawable="#drawable/switch_thumb_holo_light" />
</selector>
This defines the thumb drawable, the image that is moved over the background. There are four ninepatch images used for the slider:
The deactivated version (xhdpi version that Android is using)
The pressed slider:
The activated slider (on state):
The default version (off state):
There are also three different states for the background that are defined in the following selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/switch_bg_disabled_holo_dark" />
<item android:state_focused="true" android:drawable="#drawable/switch_bg_focused_holo_dark" />
<item android:drawable="#drawable/switch_bg_holo_dark" />
</selector>
The deactivated version:
The focused version:
And the default version:
To have a styled switch just create this two selectors, set them to your Switch View and then change the seven images to your desired style.
It's an awesome detailed reply by Janusz. But just for the sake of people who are coming to this page for answers, the easier way is at http://android-holo-colors.com/ (dead link) linked from Android Asset Studio
A good description of all the tools are at AndroidOnRocks.com (site offline now)
However, I highly recommend everybody to read the reply from Janusz as it will make understanding clearer. Use the tool to do stuffs real quick
You can customize material styles by setting different color properties.
For example custom application theme
<style name="CustomAppTheme" parent="Theme.AppCompat">
<item name="android:textColorPrimaryDisableOnly">#00838f</item>
<item name="colorAccent">#e91e63</item>
</style>
Custom switch theme
<style name="MySwitch" parent="#style/Widget.AppCompat.CompoundButton.Switch">
<item name="android:textColorPrimaryDisableOnly">#b71c1c</item>
<item name="android:colorControlActivated">#1b5e20</item>
<item name="android:colorForeground">#f57f17</item>
<item name="android:textAppearance">#style/TextAppearance.AppCompat</item>
</style>
You can customize switch track and switch thumb like below image by defining xml drawables. For more information http://www.zoftino.com/android-switch-button-and-custom-switch-examples
Alternative and much easier way is to use shapes instead of 9-patches.
It is already explained here:
https://stackoverflow.com/a/24725831/512011

Support library for text color behaviour of API 26?

API 26 introduces an advanced color calculation for ?textColorPrimary based on ?colorForeground. It makes use of states, primaryContentAlpha and disabledAlpha.
sdk/platforms/android-26/data/res/color/text_color_primary.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:alpha="?attr/disabledAlpha"
android:color="?attr/colorForeground"/>
<item android:alpha="?attr/primaryContentAlpha"
android:color="?attr/colorForeground"/>
</selector>
On API 23 it falls back to white text by means I failed to figure out.
Is there a support library I could apply to get the color calculation of API 26 for older devices?
#eugen-pechanec is hinting that the attributes primaryContentAlpha and scondaryContentAlpha are missing, IMHO below API 26. Should we call this a bug or a missing back port? Don't know.
The consequence is that you can't use the setting ?attr/colorForeground as a default to automatically create all foreground colours out of the box. You basically have two options, either not to use it to to do a manual back port.
Disable colorForeground
Instead of generating the colours from ?attr/colorForeground you set the attributes android:textColorPrimary and android:textColorSecondary directly. This will be the best choice in most cases.
Backport colorForeground
If you plan to use a lot of different themes, you want to enable the feature to set defaults for all text colours in a central place. Then you have to implement the behaviour of API 26 in your root theme.
root theme:
<!-- workaround to port back API 26+ behaviour -->
<!-- below 26 these two attributes are missing in the android namespace -->
<item name="primaryContentAlpha">1.0</item>
<item name="secondaryContentAlpha">.85</item>
<!-- works below 26 -->
<item name="android:disabledAlpha">.4</item>
<!-- use my own files to connect my custom attributes -->
<item name="android:textColorPrimary">#color/text_color_primary</item>
<item name="android:textColorSecondary">#color/text_color_secondary</item>
app/src/main/res/color/text_color_primary.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:alpha="?android:disabledAlpha" android:color="?android:attr/colorForeground" />
<item android:alpha="?attr/primaryContentAlpha" android:color="?android:attr/colorForeground" />
</selector>
app/src/main/res/color/text_color_secondary.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:alpha="?android:disabledAlpha" android:color="?android:colorForeground"/>
<item android:alpha="?secondaryContentAlpha" android:color="?android:colorForeground"/>
</selector>

How can I style an Android Switch?

The switch widget introduced in API 14 is styled by default with holo theme.
I want to style it slightly different, changing its colors and shape a bit for branding reasons. How does one go about this? I know it must be possible, as ive seen the difference between default ICS and Samsung's touchwiz theme
I assume I'll need some state drawables, and I've seen a few styles in http://developer.android.com/reference/android/R.styleable.html with Switch_thumb and Switch_track that look like what I might be after. I just don't know how to go about using them.
I'm using ActionbarSherlock if that makes a difference. Only devices running API v14 or above will be able to use a switch at all, of course.
You can define the drawables that are used for the background, and the switcher part like this:
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:thumb="#drawable/switch_thumb"
android:track="#drawable/switch_bg" />
Now you need to create a selector that defines the different states for the switcher drawable.
Here the copies from the Android sources:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/switch_thumb_disabled_holo_light" />
<item android:state_pressed="true" android:drawable="#drawable/switch_thumb_pressed_holo_light" />
<item android:state_checked="true" android:drawable="#drawable/switch_thumb_activated_holo_light" />
<item android:drawable="#drawable/switch_thumb_holo_light" />
</selector>
This defines the thumb drawable, the image that is moved over the background. There are four ninepatch images used for the slider:
The deactivated version (xhdpi version that Android is using)
The pressed slider:
The activated slider (on state):
The default version (off state):
There are also three different states for the background that are defined in the following selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/switch_bg_disabled_holo_dark" />
<item android:state_focused="true" android:drawable="#drawable/switch_bg_focused_holo_dark" />
<item android:drawable="#drawable/switch_bg_holo_dark" />
</selector>
The deactivated version:
The focused version:
And the default version:
To have a styled switch just create this two selectors, set them to your Switch View and then change the seven images to your desired style.
It's an awesome detailed reply by Janusz. But just for the sake of people who are coming to this page for answers, the easier way is at http://android-holo-colors.com/ (dead link) linked from Android Asset Studio
A good description of all the tools are at AndroidOnRocks.com (site offline now)
However, I highly recommend everybody to read the reply from Janusz as it will make understanding clearer. Use the tool to do stuffs real quick
You can customize material styles by setting different color properties.
For example custom application theme
<style name="CustomAppTheme" parent="Theme.AppCompat">
<item name="android:textColorPrimaryDisableOnly">#00838f</item>
<item name="colorAccent">#e91e63</item>
</style>
Custom switch theme
<style name="MySwitch" parent="#style/Widget.AppCompat.CompoundButton.Switch">
<item name="android:textColorPrimaryDisableOnly">#b71c1c</item>
<item name="android:colorControlActivated">#1b5e20</item>
<item name="android:colorForeground">#f57f17</item>
<item name="android:textAppearance">#style/TextAppearance.AppCompat</item>
</style>
You can customize switch track and switch thumb like below image by defining xml drawables. For more information http://www.zoftino.com/android-switch-button-and-custom-switch-examples
Alternative and much easier way is to use shapes instead of 9-patches.
It is already explained here:
https://stackoverflow.com/a/24725831/512011

Default Eclipse Theme for Android Development Spinners?

The color of my Spinner widgets change from phone to phone based on there settings,I would like to keep them the same between all phones and I would like to have it be the default eclipse uses in its emulators the color is silverish. Can you please let me know how to apply this specific theme to my Spinner in xml. Thanks in advance.
download the images used in for the spinner from the android source code: spinner_press.9.png, spinner_select.9.png, spinner_normal.9.png then create selector using those.
this is the default selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/spinner_press" />
<item android:state_pressed="false" android:state_focused="true"
android:drawable="#drawable/spinner_select" />
<item android:drawable="#drawable/spinner_normal" />
</selector>
You don't need to create a theme. Just apply a background of your choice to the spinner, as with any other View.
If you want to have different backgrounds you can use a (See here for more info)
For an example of an implementation, nothing better than the android source: https://android.googlesource.com/platform/frameworks/base/+/froyo-release/core/res/res/drawable/spinner_background.xml
You can get the actual 9-patch files from there also (and choose which version you would like to use).

Categories

Resources