9-patch drawable shows different on android 2.+ and 4.+ - android

I have selector for list which is based on 9-patch drawable but it looks different on android 2.+ and 4+ android based devices (LG Optimus Sol E730, Nexus 4, HTC One V):
Result for android 2+
Result for android 4+
Here is code of selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/row_widget_selected" android:state_selected="true"/>
<item android:drawable="#drawable/row_widget_selected" android:state_pressed="true"/>
<item android:drawable="#drawable/row_widget_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/row_widget"/>
</selector>
Use of selector:
<com.ssbs.sw.SWE.widget.SpinnerWidget
android:id="#+id/dialog_outlet_task_edit_spinner_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?listWidgetSelector"
And selector 9-patch drawable
Q: As you can see the same selector looks different on android 4+, it is a gradient why is it so?

I have found answer on my question here Android 4.2 9-patch background drawn differently than older OS versions
The easiest way to solve it is to follow the #CommonsWare proposal and move all my drawable to res/drawable-nodpi/ folder

Related

Migration of exisitng splashScreen to Android 12 + is giving distorted splash

We are trying to migrate (because of a crash, that seem related to this - Here's another question for my crash.
Our existing Splash-Screen which also serves as routing.
Previously we used a full screen vector as a theme of splash screen, after following the documentation -
https://developer.android.com/develop/ui/views/launch/splash-screen/migrate#prevent_the_custom_activity_from_displaying
and updated the theme like this -
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenAnimatedIcon">#drawable/ic_launcher</item>
<item name="background">#drawable/background_splash</item>
<item name="postSplashScreenTheme">#style/AppTheme</item>
</style>
background_splash is the drawable image that we want to have in full screen there. Its not working?
Splash screen is looking distorted, the windowSplashScreenAnimatedIcon is cropped and put into a small circle in the center.
Anyone has idea how to achieve this correctly? Thank you for your time!
Is it possible to provide a full screen drawable to splash in accordance with the new splash API for android 12+ with backward compatibility?
Searched for a solution working with Android-12 as well as with older versions and didn't like to create multiple resolution images. Android-12 changed the behaviour of the splash screen (migration guide) but this resulted in a well sized icon either for Android-12 or for earlier versions, but not for both.
Finally I ended up with a trick, to add two splash screen xml files, one for version 12 and higher, the other for all other versions. Give the item (parent of the image) a size in dp, and the bitmap a gravity="fill". The size differs for both splash_screen xml, I don't know if there is an official solution to this problem.
resources/values/styles.xml
<style name="Theme.App.Starting" parent="Theme.SplashScreen">
<item name="windowSplashScreenBackground">#387aa8</item>
<item name="windowSplashScreenAnimatedIcon">#drawable/splash_screen</item>
<item name="postSplashScreenTheme">#style/MainTheme</item>
</style>
resources/drawable/splash_screen.xml
<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="224dp" android:height="224dp" android:gravity="center">
<bitmap
android:src="#drawable/silent_notes_256"
android:gravity="fill"/>
</item>
</layer-list>
resources/drawable-v31/splash_screen.xml (Android-12 and higher)
<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:width="128dp" android:height="128dp" android:gravity="center">
<bitmap
android:src="#drawable/silent_notes_256"
android:gravity="fill"/>
</item>
</layer-list>
While I did tests with different resolutions, I don't know whether this works for all screen size/resolution combinations.

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

Spinner background is not correct on Android 6.0+

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

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

Nine-patch StateListDrawable rendering issue

After updating to SDK tools revision 15 from revision 12, my EditTexts and Buttons all appear distorted like so...
Now is this a bug with the revision? Because when I preview what the layout looks like with the "Graphical Layout" tab it appears normal. But as soon as it is compiled and put on my phone or the emulator, it is distorted.
Here is the StateListDrawable xml for the buttons. Each drawable referenced in this is a 9patch. The EditTexts are done in the same fashion.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_grey_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/button_grey_pressed" android:state_focused="true"></item>
<item android:drawable="#drawable/button_grey_pressed" android:state_selected="true"></item>
<item android:drawable="#drawable/button_grey_default"></item>
</selector>
This is an issue I've had too when I updated to SDK 14.
I found a solution in Window>Preferences>Android>Build by setting "Build output" on Normal and then Project>Clean>Clean All.
I've copied and pasted from here and it worked:
Disable auto-refresh, build automatically.
Full clean all projects.
Build all projects.
Clean the main project.
Build the main project.
Try wrapping those nine-patch drawables (I suppose #drawable/button_grey_pressed refers to /drawable/button_grey_pressed.9.png) into xml nine-patch drawables:
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/button_grey_pressed"
android:dither="true" />
You will have to give this xml file the name which will be different from that of button_grey_pressed.9.png, let's say button_grey_pressed9.xml

Categories

Resources