Android Question Replace a view

nobbi59

Active Member
Licensed User
Longtime User
Hey there,

Im a bit confused, because the code I am writing does not work. I am trying to replace a view by a new view of the same type.

Here is my code:
B4X:
For Each v As View In act.GetAllViewsRecursive
      If Not(v.Tag = Null) Then
         vmap = v.Tag
         If vmap.Get("name") = "testbutton" Then
            Dim newbt as Button
            newbt = CreateNewButton(vmap)   
            v = newbt
         End If
      End If
Next

Am I doing something wrong or is it just not possible to replace a view?

Only for understanding my problem:
- I do not want to remove the view and then add it again, because this changes the z-order.
- I do not want to just set v.Color because i want to change more fields that are defined within the Map called "vmap"
 

Emme Developer

Well-Known Member
Licensed User
Longtime User
I don't think you can simply replace the view, you need to remove current view and add new view.
With v = newbt you are switching the pointer of object v to a new view, but you cannot replace the view in this way.. try a code like this



B4X:
dim found as boolean = false
For i = 0 to act.numberofview-1
     dim v as view = act.getview(i)
      If Not(v.Tag = Null) Then
         vmap = v.Tag
         If vmap.Get("name") = "testbutton" Then
            Dim newbt as Button
            newbt = CreateNewButton(vmap)   'don't know what this do, i suppose make button
            act.addview(newbt,v.left,v.top,v.width,v.height)
           v.removeview
          found = true
        else
           if found then v.sendToBack
         End If
      End If
Next
 
Last edited:
Upvote 0

nobbi59

Active Member
Licensed User
Longtime User
Thank you Emme,

I didnt want to do this because of the z-order. But I think I will have to.

Thanks for your help :)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Create a helpersub which CHANGES a Button with all what you want to change.

and then you just change the already defined button.

something like
B4X:
For Each v As View In act.GetAllViewsRecursive
    if v is Button then
      If Not(v.Tag = Null) Then
         vmap = v.Tag
         If vmap.Get("name") = "testbutton" Then
            Dim buttontochange as Button = v ' get a reference to the right button
            ChangeButton(buttontochange,vmap)
         End If
    end if
      End If
Next
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
Thank you Emme,

I didnt want to do this because of the z-order. But I think I will have to.

Thanks for your help :)
I write a little code that should mantains your z order after exit the cycle.. you should try, I did not try it so I don't know if it works
 
Upvote 0
Top