Android Code Snippet Switch Colors

SS-2018-05-07_09.08.12.png


B4X:
#AdditionalJar: com.android.support:support-compat

Sub SetSwitchColors (Switch As Switch, On As Int, Off As Int)
   Dim p As Phone
   If p.SdkVersion < 16 Then Return
   Dim DrawableCompat As JavaObject
   DrawableCompat.InitializeStatic("android.support.v4.graphics.drawable.DrawableCompat")
   Dim jo As JavaObject = Switch
   DrawableCompat.RunMethod("setTintList", Array(jo.RunMethod("getThumbDrawable", Null), CreateColorStateList(On, Off)))
End Sub

'Based on: https://www.b4x.com/android/forum/threads/colorstatelist.40788/#content
Sub CreateColorStateList(On As Int,Off As Int) As Object
   Dim sd As StateListDrawable 'ignore
   Dim States(2,1) As Int
   States(0,0) = sd.State_Checked
   States(1,0) = sd.State_Enabled
   Dim CSL As JavaObject
   CSL.InitializeNewInstance("android.content.res.ColorStateList",Array(States, Array As Int(On, Off)))
   Return CSL
End Sub



Depends on: JavaObject, Phone
 

appie21

Active Member
Licensed User
Longtime User
i want add colors to my switch
Dim SwNoti As Switch
How to activate these code (on that switch)?
 

mmieher

Active Member
Licensed User
Longtime User
SS-2018-05-07_09.08.12.png


B4X:
#AdditionalJar: com.android.support:support-compat

Sub SetSwitchColors (Switch As Switch, On As Int, Off As Int)
   Dim p As Phone
   If p.SdkVersion < 16 Then Return
   Dim DrawableCompat As JavaObject
   DrawableCompat.InitializeStatic("android.support.v4.graphics.drawable.DrawableCompat")
   Dim jo As JavaObject = Switch
   DrawableCompat.RunMethod("setTintList", Array(jo.RunMethod("getThumbDrawable", Null), CreateColorStateList(On, Off)))
End Sub

'Based on: https://www.b4x.com/android/forum/threads/colorstatelist.40788/#content
Sub CreateColorStateList(On As Int,Off As Int) As Object
   Dim sd As StateListDrawable 'ignore
   Dim States(2,1) As Int
   States(0,0) = sd.State_Checked
   States(1,0) = sd.State_Enabled
   Dim CSL As JavaObject
   CSL.InitializeNewInstance("android.content.res.ColorStateList",Array(States, Array As Int(On, Off)))
   Return CSL
End Sub



Depends on: JavaObject, Phone
Update: I just used this with success without adding #AdditionalJar: com.android.support:support-compat
 
Top