Android Question CustomListView and Hololight theme

Discussion in 'Android Questions' started by Alberto Michelis, Jun 20, 2015.

  1. Alberto Michelis

    Alberto Michelis Active Member Licensed 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The "pressed" color disappears in all themes. The colors are managed by StateListDrawable (line #135).
     
  3. Alberto Michelis

    Alberto Michelis Active Member Licensed User

    You mean I cant change it? or
    Where to find StateListDrawable to modify line 135?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    I mean that the expected behavior is that the background color disappears. If you like to change the behavior then you need to modify the code. The relevant code starts in line #135.
     
  5. Alberto Michelis

    Alberto Michelis Active Member Licensed User

    Sory I need more help line 135 of what? of the custom list view code?
    Im lost...
     
  6. RandomCoder

    RandomCoder Well-Known Member Licensed User

    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...
    Code:
    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, 
    00, sv.Width, ItemHeight)
        p.Tag = Index
    ....
    ....
    ....
    Where pressedDrawable was set as follows...

    Code:
    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.

    Code:
    'My call to get the StateListDrawable
    btnEnter.Background  = getButtonGradient(Array As Int(Colors.RGB(5020550), 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
     
    DonManfred, mangojack and LucaMs like this.
Loading...