Android Question CustomListView change panel(list) background color when clicked

phukol

Active Member
Licensed User
Longtime User
Hi guys, just want to ask how can i change the background color of my CustomListView when i click or press one of the list. Currently, its constantly giving me the orange color.
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    23 KB · Views: 309

eurojam

Well-Known Member
Licensed User
Longtime User
you can do something similar to this, it is just an example, not tested;
first you add some Panels to your customlistview, like
B4X:
...
    For i = 1 To 10
        Dim p As Panel
        p.Initialize("clv_panel")
        clv1.Add(p, 50dip, "Item #" & i)
    Next
...
then you define the panel_click sub like
B4X:
Sub clv_panel_Click
    Dim p As Panel
    p = Sender
    p.Color = Colors.Green
End Sub
that's it, more or less
 
Upvote 0

phukol

Active Member
Licensed User
Longtime User
you can do something similar to this, it is just an example, not tested;
first you add some Panels to your customlistview, like
B4X:
...
    For i = 1 To 10
        Dim p As Panel
        p.Initialize("clv_panel")
        clv1.Add(p, 50dip, "Item #" & i)
    Next
...
then you define the panel_click sub like
B4X:
Sub clv_panel_Click
    Dim p As Panel
    p = Sender
    p.Color = Colors.Green
End Sub
that's it, more or less

Thank you that will work, however, when you click on the panel, they will turn to green but if ever you select and click another panel, it will also turn to green. Also the click event of customlist view will be disabled. Im thinking that it would be better if we change the code in customlistview to change the actual click event for it.
 

Attachments

  • Untitled.png
    Untitled.png
    16 KB · Views: 278
Upvote 0

phukol

Active Member
Licensed User
Longtime User
I looked around and found out that i need to change this:
idPressed = r.GetStaticField("android.R$drawable", "list_selector_background")

"list_selector_background" to "list_pressed_holo_dark"

however i need to use the android resources library to access list_pressed_holo_dark? can anyone point me how i can do this?
 
Upvote 0

phukol

Active Member
Licensed User
Longtime User
For anyone having the same issue kindly use NinePatch Drawabales tutorial. What i did is make a sub which returns a drawable like this
B4X:
Public Sub SetNinePatchDrawable( ImageName As String) As BitmapDrawable
   Dim r As Reflector
   Dim package As String
   Dim id As Int
   package = r.GetStaticField("anywheresoftware.b4a.BA", "packageName")
   id = r.GetStaticField(package & ".R$drawable", ImageName)
   r.Target = r.GetContext
   r.Target = r.RunMethod("getResources")
   Return  r.RunMethod2("getDrawable", id, "java.lang.int")
  
End Sub

Then i made use of a png background placed inside Objects > res > drawable. Set it to read only and just call it's file like this
B4X:
pressedDrawable = Utils.SetNinePatchDrawable("list_selector_pressed_holo_dark")

And problem fixed.
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
@phukol , can you explain how you modify:
B4X:
    idPressed = r.GetStaticField("android.R$drawable", "list_selector_background")
To something else, in order to get another backcolor, when an item of the CustomListView is clicked

Thanks a lot
 
Upvote 0
Top