Android Question how to get listview1 itemcolor?

xiaoyao

Active Member
Licensed User
Longtime User
can't run
B4X:
Sub GetSystemTextColor As Int
    Dim co As Int

    Dim typedValue As JavaObject
    typedValue.InitializeNewInstance("android.util.TypedValue", Null)
    
    Dim theme As JavaObject = Activity
    theme = theme.RunMethod("getTheme", Null)
    Dim value As Boolean = theme.RunMethod("resolveAttribute", Array As Object(android.R.attr.textColorPrimary, typedValue, True))
    
    If value Then
        Return typedValue.GetField("data")
    Else
        Return Colors.Black '
    End If
    
End Sub
 

teddybear

Well-Known Member
Licensed User
1. As Erel suggested, why don't you use customlistview instead of listview?
2. The code looks like it was generated by AI. it seems to only retrieve the theme's textColorPrimary, and has nothing to do with the listview.
3. If you would like to get the textColorPrimary, you need to initialize the object android.R.attr instead of passing a literal "android.R.attr.textColorPrimary" as a parameter.
4. You'd better post full error log. I guess it's not "can't run" but "can't compile"
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
can't run
B4X:
Sub GetSystemTextColor As Int
    Dim co As Int

    Dim typedValue As JavaObject
    typedValue.InitializeNewInstance("android.util.TypedValue", Null)
 
    Dim theme As JavaObject = Activity
    theme = theme.RunMethod("getTheme", Null)
    Dim value As Boolean = theme.RunMethod("resolveAttribute", Array As Object(android.R.attr.textColorPrimary, typedValue, True))
 
    If value Then
        Return typedValue.GetField("data")
    Else
        Return Colors.Black '
    End If
 
End Sub
Without knowing what you are exactly doing it's not easy to answer.
If you are adding simple items like
B4X:
ListView1.AddSingleLine("HELLO")
then you can do this to get the text color
B4X:
Dim Label1 As B4XView = ListView1.SingleLineLayout.Label
Log(Label1.TextColor)    'Color of the Text
Log(Label.Color)        'Color of the Item
supposing that ALL the items have the same color.
 
Upvote 0
Top