Android Question Scrollbar Color vs Android 12

T201016

Active Member
Licensed User
Longtime User
Hi,
The following color change example works well on lower versions of Android because "android.graphics.drawable.drawable" is an abstract class and is based on a private API which in higher versions of Android throws an error: noSuchMethodException.

I am looking for a working code for android version 12, could someone help me, thank you very much in advance.

'noSuchMethodException: >= SDK30:
Public Sub SetScrollbarColor(sv As ScrollView, clr As Int)
    Dim r As Reflector
    r.Target = sv
    r.Target = r.GetField("mScrollCache")
    r.Target = r.GetField("scrollBar")
    Dim cd As ColorDrawable
    cd.Initialize(clr, 5dip)
    r.RunMethod4("setVerticalThumbDrawable", Array(cd), Array As String("android.graphics.drawable.Drawable"))
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Private Sub ChangeScrollbarColor(v As B4XView, Drawable As Object)
    Dim p As Phone
    If p.SdkVersion >= 29 Then
        v.As(JavaObject).RunMethod("setVerticalScrollbarThumbDrawable", Array(Drawable))
    End If
End Sub

Usage:
B4X:
Dim cd As ColorDrawable
    cd.Initialize(xui.Color_Red, 5dip)
    ChangeScrollbarColor(ScrollView1, cd)

Note that it will work with xCLV as well. You need to pass clv.sv as the view.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I applied Erel's snippet to an xCLV and it works well indeed.
B4X:
Dim cd As ColorDrawable
    cd.Initialize(xui.Color_Red, 10dip)
    ChangeScrollbarColor(CustomListView1.sv, cd)
1681988066639.png
 
Upvote 0

Similar Threads

Top