Android Question StateListDrawable get state drawable?


Well-Known Member
Licensed User
Im trying to get hold of the selection colour from my list view (since the selection colour depends on the platform).

Anyway Ive got the StateListDrawable back from the list view.

How do I get a colour drawable back from the State_Selected?


Well-Known Member
Licensed User

Thanks. The code below works if the index is hard coded to 3.

Dim LVO As JavaObject = lvMain
Dim SLD As StateListDrawable = LVO.RunMethod("getSelector",Null)
Dim jo As JavaObject = SLD
Dim drawable As Object = jo.RunMethod("getStateDrawable", Array(3))

lvMain.SingleLineLayout.Background = drawable

SLD.State_Selected returns a high number (~15000) rather than the index. Ive tried calling getStateDrawableIndex but am seeing an "unmatched" error.

Upvote 0


Well-Known Member
Licensed User
Wow what a mission! So what I didnt realise was that the StateListDrawable I was interested in was animated (for a transition).

If you want a static background that doesnt react to user interaction then you need to get the actual drawable.

Resulting code below. This sets a list views single layout background to be the same as the "pressed" colour. The reason for this is that I am using the single line items as headers in the list view. Setting the background this way makes it appear that the items are not pressable since they dont change colour.

Sub SetSingleItemBackground()   
    Dim StateIndex As Int = -1
    Dim StateCount As Int   
    Dim LVO As JavaObject = lvBridgeFinder
    Dim SLD As StateListDrawable = LVO.RunMethod("getSelector",Null)
    Dim SLDO As JavaObject = SLD   
    StateCount = SLDO.RunMethod("getStateCount", Null)   
    Dim StateSet() As Int
    Dim n As Int
    For n = 0 To StateCount-1       
        StateSet = SLDO.RunMethod("getStateSet", Array(n))
        If StateSet.Length = 2 Then
            If StateSet(1) = 16842919 Then '16842919 from
                StateIndex = n
            End If
        End If       
    If StateIndex <> -1 Then
        Dim oTransitionDrawable As Object = SLDO.RunMethod("getStateDrawable", Array(StateIndex))       
        Dim TDO As JavaObject = oTransitionDrawable
        Dim oDrawable As Object = TDO.RunMethod("getDrawable", Array(1))
        lvBridgeFinder.SingleLineLayout.Background = oDrawable
    End If
End Sub
Upvote 0