Bug in RemoveViewAt with Gradient-Background-Label?

Quillok

Member
Licensed User
Longtime User
Hi, I'm having a problem, I'm not sure if it's a bug or if I am doing something wrong, but maybe someone of you could help me.

B4X:
Sub Globals      
   Dim Labels(11) As Label
   Dim GoButton As Button 
   Dim DelButton As Button
   Dim Gradient As GradientDrawable 
   Dim BackColor(2) As Int   
End Sub

Sub Activity_Create(FirstTime As Boolean)   
   GoButton.Initialize("GoButton")
   GoButton.Text = "Add"
   Activity.AddView(GoButton,0,0,200,100)
   
   DelButton.Initialize("DelButton")
   DelButton.Text = "Delete"
   Activity.AddView(DelButton,200,0,200,100)
   
   BackColor(0) = Colors.Yellow 
   BackColor(1) = Colors.Green
   
   Gradient.Initialize("TOP_BOTTOM",BackColor)   
End Sub

Sub GoButton_click
   For i = 1 To 10 
      Labels(i).Initialize("abc")
      Labels(i).Text = i
      Labels(i).TextColor = Colors.White
      Labels(i).Background  = Gradient
      'Labels(i).Color = Colors.Yellow 
      Activity.AddView(Labels(i),Rnd(0,200),Rnd(100,500),Rnd(50,100),Rnd(50,100))
   Next
End Sub

Sub DelButton_click
   Activity.RemoveViewAt(Activity.NumberOfViews-1)
End Sub

Problem: As you can see I want to add some random Labels, and then, if I click on the button to delete, the last view on the activity is deletet.
But my problem is, when i click it, the size of the other labels is also changed, I don't know why, but when I set the backgroundcolor to yellow for example (that line that is uncomment at the moment), it works fine without resizing the other labels.

Is this a bug? Or am I doing something wrong? Is there something wrong with the gradient function? Would be great if someone could help me, because i don't know where the mistake can be :/

I'll tried it on my Samsung Galaxy S2, got no other phone to check if it's the fault of the phone...
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The problem is that you are using the same drawable object for all views. It causes this issue when a view is removed.
The solution is to initialize the gradient before adding a new view:
B4X:
Sub GoButton_click
    For i = 1 To 10 
        Labels(i).Initialize("abc")
        Labels(i).Text = i
        Labels(i).TextColor = Colors.White
      Gradient.Initialize("TOP_BOTTOM",BackColor)    
        Labels(i).Background  = Gradient
        'Labels(i).Color = Colors.Yellow 
        Activity.AddView(Labels(i),Rnd(0,200),Rnd(100,500),Rnd(50,100),Rnd(50,100))
    Next
End Sub
 
Top