Android Question CustomListView and Hololight theme

Alberto Michelis

Well-Known Member
Licensed User
Longtime User
Hi,
Using the Holo-Light theme with a CustomListView the color indicator when I click the ite goes away.
Not using the theme I get a yellow backgroun color when I click in the item.
I want it (not in yellow) to go back.
How can I do it?
Thanks
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Sory I need more help line 135 of what? of the custom list view code?
Im lost...
Yes in the CustomListView code, it's where the pressed state is set and Erel originally used a method to get the pressed state based on the theme...
B4X:
Public Sub InsertAt(Index As Int, Pnl As Panel, ItemHeight As Int, Value As Object)
   
    Dim sd As StateListDrawable
    sd.Initialize
    sd.AddState(sd.State_Pressed, pressedDrawable)
    sd.AddCatchAllState(Pnl.Background)
   
    'create another panel to handle the click event
    Dim p As Panel
    p.Initialize("panel")
    p.Background = sd
    Dim cd As ColorDrawable
    cd.Initialize(Colors.Transparent, 0)
    Pnl.Background = cd
    p.AddView(Pnl, 0, 0, sv.Width, ItemHeight)
    p.Tag = Index
....
....
....

Where pressedDrawable was set as follows...

B4X:
    Dim R As Reflector
    Dim idPressed As Int
       idPressed = R.GetStaticField("android.R$drawable", "list_selector_background")
    R.Target = R.GetContext
    R.Target = R.RunMethod("getResources")
    pressedDrawable = R.RunMethod2("getDrawable", idPressed, "java.lang.int")

@Erel likes fancy code :D, a simple way to set the states is shown below (copied from elsewhere on the Forum but I forget where, I only know that Roger Garstang originally provided it as I always give credit to the original author of code snippets in all of my Apps). You can modify the pressed state to be whatever you want.

B4X:
'My call to get the StateListDrawable
btnEnter.Background  = getButtonGradient(Array As Int(Colors.RGB(50, 205, 50), Colors.RGB(0,128,0)))
.....
.....

'The sub which returns a StateListDrawable
Private Sub getButtonGradient(intColorList() As Int) As StateListDrawable ' Courtesy of B4A user Roger Garstang
    ' Define GradientDrawable for Enabled state
    Dim gdwEnabled As GradientDrawable
    gdwEnabled.Initialize("TOP_BOTTOM", intColorList)
    gdwEnabled.CornerRadius = 5

    ' Define GradientDrawable for Pressed state
    Dim gdwPressed As GradientDrawable
    gdwPressed.Initialize("LEFT_RIGHT", intColorList)
    gdwPressed.CornerRadius = 10

    ' Define StateListDrawable
    Dim stdGradient As StateListDrawable
    stdGradient.Initialize
    stdGradient.AddState2(Array As Int(stdGradient.State_enabled, -stdGradient.State_Pressed), gdwEnabled)
    stdGradient.AddState(stdGradient.State_Pressed, gdwPressed)
    Return stdGradient
End Sub
 
Upvote 0
Top