Android Question CustomListview Gradient background colors at runtime ?

Addo

Well-Known Member
Licensed User
Longtime User
i have did like following

B4X:
Dim sv As ScrollView = clv.sv
Dim gd(2) As Int
gd(0) = Colors.RGB(0, 95, 170)
gd(1) = Colors.RGB(0, 190, 255)
Dim gdContent As GradientDrawable
gdContent.Initialize("LEFT_RIGHT", gd)

sv.Background = gdContent

the clv background changed but the items inside the clv have a diffrent color any idea why ?
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
should i loop through clv items and set each item panel background as gradient as well or there is more simple way ?

You could do that ... Or

In your CLV Item layout place views on a Panel and set the background tp GradientDrawable ... Or

Color the Item Panel when loading items to the CLV.
B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
    Dim p As Panel
    p.Initialize("")
    'set panel color gradient  ... Not tested.!
 
Upvote 0

Addo

Well-Known Member
Licensed User
Longtime User
I think its better to use a timer in my case sense I will change the clv colors each 1 minute.

Also timer could be a good choice sense each time I put the application to background or open the keyboard the clv gradient background cleared and had to be re drawed.
 
Upvote 0

Addo

Well-Known Member
Licensed User
Longtime User
@mangojack i have also tried to do the same method you have posted

like following


B4X:
Sub CreateListItem(Text As String, Width As Int, Height As Int) As Panel
Dim itempnl As Panel

itempnl.Initialize("")
itempnl.LoadLayout("itemlayout")


Dim gd(2) As Int
gd(0) = Colors.White
gd(1) = Colors.Blue
Dim gdContent As GradientDrawable
gdContent.Initialize("LEFT_RIGHT", gd)
itempnl.Background = gdContent


Return itempnl
  
  
End Sub


i got this exception when ever i try to set background

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.res.ColorStateList.getDefaultColor()' on a null object reference

what iam doing wrong ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Never call LoadLayout on a zero sized panel.
B4X:
Dim itempnl As B4XView = XUI.CreatePanel("")
itempnl.SetLayoutAnimated(0, 0, 0, Width, Height)

2. xCLV expects the root panel color to be a solid color. Add another panel in the layout that is anchored to BOTH sides and then change its background.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…