Android Question B4XViews and back and forward [Solved in a crude manner]

Roger Daley

Well-Known Member
Licensed User
Longtime User
OK I'm thick,

Erel kindly posted a couple of Subs for moving views backward and forward in their Z order.
These Subs use B4XViews, I have never used the B4X commands and thought this should be easy. After an afternoon of reading through stuff on the forum I have succeeded in achieving nothing. Resisting the urge to throw the computer out the window I am doing the shameful act of attaching a sample program and asking where do I really start?

Regards Roger
 

Attachments

  • MoveViews.zip
    100.9 KB · Views: 312

LucaMs

Expert
Licensed User
Longtime User
1 - in your code, you call:
B4X:
sendViewForwards(Label2x, p1)
where p1 is a label but sendViewForwards expects the label to move and its parent-container.

2 - you need to place the labels into a pane(l), otherwise all the views will be considered, even the two buttons.

3 - To test I used B4J. If you run this code:
B4X:
    Dim i As Int
    For Each xView As B4XView In BaseOfViews.GetAllViewsRecursive ' BaseOfViews is the pane in which I placed the six labels to work on.
        Log(i)
        i = i + 1
    Next
    Log(i)
the log will show 11 (base 0), not 5. This is because B4XViews are made of two views and GetAllViewsRecursive does not work as we need with them.

Anyway, I managed to get what you need, with the attached project (it is b4j but it will be easy to adapt it for b4a).

(This just because I like to create animated gif from screen ☺)



P.S. Attached also the b4a version.


[it would be possible to change the source to have a single routine, but I don't have time to do it now]
* Done and attached.
 

Attachments

  • ChangeViewZOrder.zip
    3 KB · Views: 284
  • ChangeViewZOrder_B4A.zip
    10.1 KB · Views: 288
  • ChangeViewZOrder_B4A_SingleRoutine.zip
    10.2 KB · Views: 289
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Done and attached.
I am like many others still working to assimilate B4XViews. I think you could eliminate a couple of lines in your code:
B4X:
Private Panel1 As B4XView
'    Private BaseOfViews As B4XView 'not needed
    Private mlstViews As List ' List of views to work on.
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("layMain")
'    BaseOfViews = Panel1  'not needed
     mlstViews.Initialize
    For Each xView As B4XView In Panel1.GetAllViewsRecursive
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
LucaMs,
I am over whelmed, you have done a huge job in answering my question. It's early morning here so I am yet go through the detail.
One small point, puting the views in my example makes sense but in the real situation not practical.

To put the question in context here is the brief history. I did not include it in the original post as I thought it may confuse the question
1. I was after a way to improve the inadequate EditText cursor
2. Not surprisingly Erel had the answer. [Create Resource stuff in the Manifest and some code] It worked very well for the cursor, colour and size.
The code:
Erel code:
:
    Dim x As XmlLayoutBuilder
    x.LoadXmlLayout(Activity, "layout1")
    Dim et As EditText = x.GetView("edittext1")
    et.RemoveView
    Activity.AddView(et, 20dip, 20dip, 200dip, 50dip)

In my project the EditText is existing and created in the designer.
My version of Erels code:
My version:
    Private xcursor As XmlLayoutBuilder
    xcursor.LoadXmlLayout(Activity, "layout1")
    Idisplay = xcursor.GetView("edittext1")
    Idisplay.RemoveView
    Activity.AddView(Idisplay, Rdisplay.Left, Rdisplay.Top + Rdisplay.Height, dmiddle.Width, Rdisplay.Height)

3.
Idisplay created by the Designer is removed and recreated in code.
This Idisplay can be positioned X&Y but not Z. This led to my question on moving views back&forth.
The original Idisplay was Colors.Transparent successfully. The new Idisplay appears to be semi-transparent after Colors.Transparent.

Solve one small issue, create two more. Erel's improved cursor works so well I would not like to lose it but it is only a small issue and there is a limit on how much time you spend on small issues.


Thanks again for the detailed answer, I will work my way through it later today.

Regards Roger
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I know, I had noticed, it was a remnant, but I wanted to keep it because it is "more evident" that you can apply it to other "containers" as well.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I'm not quite sure I understood perfectly (wasn't there a way to change the appearance of the cursor via code?).
Removing and then inserting again a view it loses the "z order"? Well, and then you do the same action on all views, remove an add them again, this way you will get the original order.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
    Dim x As XmlLayoutBuilder
    x.LoadXmlLayout(Activity, "layout1")

'    Dim et As EditText = x.GetView("edittext1")
'    et.RemoveView
'    Activity.AddView(et, 20dip, 20dip, 200dip, 50dip)
   
    Dim lstViews As List
    lstViews.Initialize
    For Each xView As B4XView In Activity.GetAllViewsRecursive
        lstViews.Add(xView)
    Next
    Activity.RemoveAllViews
    For Each xView As B4XView In lstViews
        Activity.AddView(xView, xView.Left, xView.Top, xView.Width, xView.Height)
    Next
[not tested]
?????
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
LucaMs
Unfortunately no easy way to change cursor, I'm waiting for Erel to add cursor properties to the designer.

Recreating all views, 3 layouts, over 140 views in each layout. Maybe not.
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
LucaMs

Thanks again it looks interesting but may be a bit before trying.
Mother-inlaw passed away last night. Busy times

Roger
 
Upvote 0

Roger Daley

Well-Known Member
Licensed User
Longtime User
Moving views in Z axis programmatically abandoned. I wrote several View.Bringtofront lines to bypass the problem. Crude but effective.
Perhaps one day we will have View1.BringToBackOf(View2) It won't be me.
Many thanks to those who offered great support.


Regards Roger
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…