Android Question remove last label

Solution
Is there a possibility to delete the last one and befores by button
Here is a complete project where you can delete the last label either by long click of the label itself or a button. It deletes from the last button up every time you click the button or long click the label itself
1643504804112.png

Mahares

Expert
Licensed User
Longtime User
delete the selected lbl with a long_click?
This solution and changes are to remove the last inserted label with the long click.
Your project by the way crashes. You need to comment all these Label.visible lines
' Label1.Visible=False
' Label2.Visible=False
' Label3.Visible=False
' Label4.Visible=False
' Label5.Visible=False
' .....In between too
' Label20.Visible=False
' Label21.Visible=False

B4X:
Private x, i As Int   'needs added to Globals
B4X:
Private Sub Button1_Click
    Dim w =200dip, h=50dip As Float
    lbl(i).Initialize("lbl")
    Activity.AddView(lbl(i), 0,i*60dip+200dip, w, h)    
    lbl(i).Text= a
    lbl(i).TextSize = 20
    ColorLabel(lbl(i))
    x=i
    i=i+1
    If i=n Then Button1.Enabled =False
End Sub

B4X:
Private Sub lbl_LongClick
    Activity.RemoveViewAt(x+2)    'the 2 is because the labels come after Panel1 and Button1 in the activity
    i=i-1
End Sub
If you have trouble I can post your project corrected
 
Upvote 0

MroBurk

Member
Licensed User
thanks a lot @Mahares but It remove only one... If it's clicked to the other one it crashes

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.unFocus(android.view.View)' on a null object reference
 

Attachments

  • radiobutton e spinner2.zip
    10.8 KB · Views: 179
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If it's clicked to the other one it crashes

If you want to remove more than 1 label but starting from the bottom one on up, this will work. For random label removal, you will need to tweak it. I did not have a chance to look further. In that case put the corrected project and ask the members to help you for more tweaking
B4X:
Private Sub Button1_Click
    Dim w =200dip, h=50dip As Float
    lbl(i).Initialize("lbl")
    Activity.AddView(lbl(i), 0,i*60dip+200dip, w, h)
    lbl(i).Text= a
    lbl(i).TextSize = 20
    ColorLabel(lbl(i))
    lbl(i).Tag =i  'add this line
    x=i
    i=i+1
    If i=n Then Button1.Enabled =False
End Sub

B4X:
Private Sub lbl_LongClick
    Dim l As Label=Sender
    Activity.RemoveViewAt(l.tag+2)    'the 2 is because the labels come after Panel1 and Button1 in the activity
End Sub
 
Last edited:
Upvote 0

MroBurk

Member
Licensed User
@Mahares thanks!!! Is there a possibility to delete the last one and befores by button? I tried with this but it crash
B4X:
  Activity.RemoveViewAt(x+2)    'the 2 is because the labels come after Panel1 and Button1 in the activity
    i=i-1
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Is there a possibility to delete the last one and befores by button
Here is a complete project where you can delete the last label either by long click of the label itself or a button. It deletes from the last button up every time you click the button or long click the label itself
1643504804112.png
 

Attachments

  • RadioButtonsSpinnerLabels013022.zip
    10.9 KB · Views: 196
Upvote 1
Solution
Top