0

I wanna create theme for both Android 5.0 and less android 5.0 application. In first case for example I have Switch that if I don't set any background it appearance will change on Android 5.0 to like a slide button as you know,

enter image description here

but in less android 5.0 it show as on/off button.

enter image description here

when I put

android:thumb="@drawable/switch_selector"

to change thumb image and color it's fine on android 4.4 but my Switch show nothing on Android 5.0. my question is how to create a selector or theme for Switch that can show on all devices?

UPDATED

in simple way, we hava many drawable folders for each screen dpi's. I wanna know if I can set drawable for each Api's as like as for each dpi's ?!

Community
  • 1
  • 1
Mahdi
  • 6,139
  • 9
  • 57
  • 109
  • see here http://stackoverflow.com/a/6536198/5202007 or here http://stackoverflow.com/questions/8908508/program-different-layouts-for-different-versions-in-android – Mohammad Tauqir Jan 02 '16 at 15:45
  • @Tauqir my problem is not with layouts. I dont wanna create many layouts, just is there any better way to define my selector and backgrounds? – Mahdi Jan 02 '16 at 15:52
  • I edited my post. Get API version and use `switch` loop to change an image, I think some of additional libraries allows you to change icon look – piotrek1543 Jan 05 '16 at 10:53

1 Answers1

0

after my hard times with Android notifications, where I need to do one method for android devices with Android 5.x and higher and another method for pre-Lollipop devices, I would advice you to search for additional libraries like this

SwitchButton

This project provides you a convenient way to use and customise a SwitchButton widget in Android. With just resources changed and attrs set, you can create a lifelike SwitchButton of Android 5.0+, iOS, MIUI, or Flyme and so on.

Now we get the biggest movement since SwitchButton published. v1.3.0 comes with totally reconsitution and more convenient API. A wholly new demo can give you a tour in it. enter image description here

Github page: https://github.com/kyleduo/SwitchButton More like this you would find here: http://android-arsenal.com/

Additional libraries may help you to avoid problems with backward compatibilities of Android components.

Like you said, you add this line of code:

   android:thumb="@drawable/switch_selector"

and it works on Kitkat, but not on Lollipop.

I've found already that on Lollipop you may have much more to work. Just take a look here: How to have a Lollipop switch button

So try to find your desired additional library like this above and let me know how it works ;-)

EDIT: To get API version use this:

Build.VERSION.RELEASE;

That will give you the actual numbers of your version; aka 2.3.3 or 2.2. The problem with using Build.VERSION.SDK_INT is if you have a rooted phone or custom rom, you could have a none standard OS (aka my android is running 2.3.5) and that will return a null when using Build.VERSION.SDK_INT so Build.VERSION.RELEASE will work no matter what!

To use it, you could just do this;

String androidOS = Build.VERSION.RELEASE;

From: Retrieving Android API version programmatically

Community
  • 1
  • 1
piotrek1543
  • 19,130
  • 7
  • 81
  • 94
  • may using existing additional libraries work fine but what if there is no library for specific thing? I updated the question plz check. – Mahdi Jan 05 '16 at 10:35
  • 1
    OK, finally i could not find any better solution to define a custom themes for android 5.0 and below it so I accept you answer. thanks – Mahdi Jan 17 '16 at 08:11