Android Question How to remove dinamically created view

Fusseldieb

Active Member
Licensed User
Longtime User
Hi,
how can I remove a dinamically created view?
I have created panels and set a Tag for every Panel.
Now I want to remove the panel with the tag "x". How can I do this?

I have already tried this:
B4X:
For Each v As View In Activity.GetAllViewsRecursive
If v Is Panel Then
Try
If v.Tag=PanelName Then
v.RemoveView
TouchList.RemoveAt(TouchList.IndexOf(PanelName))
End If
Catch
End Try
End If
Next
But it is too slow and it gives me errors when a Tag is "null". (Atempt to invoke virtual method blablabla)
 

Fusseldieb

Active Member
Licensed User
Longtime User
Solved!
I simply created two lists.
One which stores the "Tag" and one which stores the ID (ViewIndex)
And everytime I add a panel, i fill the two panels. One with "Panel Tag" and the other with the ViewIndex (Activity.NumberOfViews).
When I want to remove a panel, I just use "IndexOf" and look for the tag I am searching. When it's encountered, I take that Index and look in the ID Panel for that same Index, and that gives me the stored View ID. And in that way I can call Activity.RemoveViewAt(x)
This method is really fast (No visible delay) because I don't go through all views, I just search through a list :)
 
Upvote 0

Fusseldieb

Active Member
Licensed User
Longtime User
To help others:

B4X:
Sub Activity_Create(FirstTime As Boolean)
    TouchListTag.Initialize()
    TouchListID.Initialize()

        AddPanel("LOL",0,0,100dip,100dip) 'Add the panel
        RemovePanel("LOL") 'Remove the panel
End Sub

Sub AddPanel(Tag as String,Left as Int,Top As Int,Width as Int,Height as Int)
        If TouchList.IndexOf(PanelName)=-1 Then
                Dim Panel1 as Panel
                Panel1.Initialize(Tag)
                Activity.AddView(Panel1,Top,Left,Width,Height)
                TouchListTag.Add(PanelName)
                TouchListID.Add(Activity.NumberOfViews)
        End If
End Sub

Sub RemovePanel(Tag as String)
        If TouchList.IndexOf(PanelName)>-1 Then
        Activity.RemoveViewAt(TouchListN.Get(TouchList.IndexOf(PanelName))-1)
        TouchListTag.RemoveAt(TouchList.IndexOf(PanelName))
        TouchListID.RemoveAt(TouchList.IndexOf(PanelName))
End Sub
 
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
Where is TouchList coming from in If TouchList.IndexOf(PanelName)>-1Then... ?
Hi,
how can I remove a dinamically created view?
I have created panels and set a Tag for every Panel.
Now I want to remove the panel with the tag "x". How can I do this?

I have already tried this:
B4X:
For Each v As View In Activity.GetAllViewsRecursive
If v Is Panel Then
Try
If v.Tag=PanelName Then
v.RemoveView
TouchList.RemoveAt(TouchList.IndexOf(PanelName))
End If
Catch
End Try
End If
Next
But it is too slow and it gives me errors when a Tag is "null". (Atempt to invoke virtual method blablabla)


Wouldn't it be easier if you just do

For Each V As View In Activity.GetAllViewsRecursive
If V.Tag<> Null Then
If V.Tag="your_panels_tag" Then
V.RemoveView
End If
End If
Next
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

JTmartins

Active Member
Licensed User
Longtime User
I mentioned that beacause of the comment : "it gives me errors when a Tag is "null", and I tottaly agree with what you said

Also my first impression was that the above code would not work properly in some situations as for example if you also have views created with the designer, but maybe I am not underdstanding the code correctly.

The way I normaly do it, only uses one List (myViews) and one INT (DesignerViews) wich holds the number of views created with designer and if by any reason we need to remove a view that was created in the designer, then we also have to decrease the DesignerViews variable.

Something like (please note that there is no error checking in this quick code)

B4X:
Sub Globals
    Private myViews As List
    Private DesignerViews As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1") ' already has some views
    DesignerViews=Activity.NumberOfViews
    myViews.Initialize

    'Add panels dynamic
    addPanel ("test1",0,0,200,200)
    addPanel ("test2",80,80,200,200)
    addPanel ("test3",160,160,200,200)
    addPanel ("test4",240,240,200,200)

    'Remove panels Dynamic and one view that was created in designer.
    removePanel ("test4")
    Activity.RemoveViewAt(1)
    DesignerViews=DesignerViews-1
    removePanel("test1")
    removePanel("test3")
    removePanel("test2")

End Sub


Sub addPanel (Pname As String, left As Int, top As Int, width As Int, height As Int )
   Private P As Panel
   P.Initialize(Pname)
   P.Tag=Pname ' if we need to reference it later we'll use tag property.
   Activity.AddView(P,left,top,width,height)
   P.Color=Colors.red ' Just to see them better
   myViews.Add(Pname)
End Sub


Sub removePanel (Pname As String)
    Activity.RemoveViewAt(myViews.IndexOf(Pname)+DesignerViews)
    myViews.RemoveAt(myViews.IndexOf(Pname))
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
 
Last edited:
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
BUT do keep in mind that in Android, views don't get destroyed, when you remove a view, you remove it from its Parent... It continues to exist and can be later added to a different Parent if needed.
 
Upvote 0
Top