Android Question [Solved] Zorder trouble...

Blacky's Boss

Member
Licensed User
Longtime User
Allright, my first post here. I'm writing an App to communicate with my (arduino-based) home automation system. Just for fun. All went well until I somehow managed to mess up the Zorder of a Button and an Imageview. Both placed on a Panel in a Scrollview.
I know it isn't rocketscience, but I can't get it to work anymore and don't know why.
Here are the important bits:
B4X:
Sub Globals
  Private Panel1 As Panel
   Private ScrollView1 As ScrollView
   
   Private Panel2(16) As Panel
   Private BTN1(16,16) As UnitButton

   Private BTN2(16) As Button
   Private Signal(16) As ImageView
   
   Private clrs As Int
   Private cdpanel As ColorDrawable
   
   Private cdConnect As BitmapDrawable
   Private cdDisconnect As BitmapDrawable
   Private cdWait As BitmapDrawable
   
   Private cdButtongroen As Bitmap
   Private cdButtonrood As Bitmap
   Private cdButtonoranje As Bitmap
   
   Private cdGroepsknop As BitmapDrawable
End Sub
B4X:
Sub Activity_Create(FirstTime As Boolean)
   'Msgbox("create","Activity")
   If FirstTime Then
    Arduino.Initialize(Me,"Arduino",8900)

   End If
     
   clrs=Colors.ARGB(100,0,0,0)
   cdpanel.Initialize(clrs,10)
      
   cdConnect.Initialize(LoadBitmap(File.DirAssets,"Actions-network-connect-icon.png"))
   cdDisconnect.Initialize(LoadBitmap(File.DirAssets,"Actions-network-disconnect-icon.png"))
   cdWait.Initialize(LoadBitmap(File.DirAssets,"Actions-view-history-icon.png"))
   cdGroepsknop.Initialize(LoadBitmap(File.DirAssets,"button_wood.png"))
   cdButtonrood.Initialize(File.DirAssets,"001.png")
   cdButtongroen.Initialize(File.DirAssets,"030.png")
   cdButtonoranje.Initialize(File.DirAssets,"015.png")
     
   Dim y1 As Int
   
   Dim breedte As Int=90%x
   
   Activity.LoadLayout("Main")
     
   Panel1.Width=90%x
   Panel1.Height=10%y
   Panel1.Top=1%y
   Panel1.Left=5%x
     
   y1=Panel1.Height
   
   ScrollView1.Height=100%y-y1
   ScrollView1.Width =100%x
   ScrollView1.Top=11%y
   ScrollView1.Left=0
   
   ScrollView1.Panel.Height=182%y
   For j=1 To 15
     Panel2(j).Initialize("Panel2")
     ScrollView1.Panel.AddView(Panel2(j),5%x,(j-1)*(12%y)+1%y,breedte,11%y)
     Panel2(j).Background=cdpanel
     
     BTN2(j).Initialize("Groepsknop")
     Panel2(j).AddView(BTN2(j),0.05*Panel2(j).Width,1%y,0.9*Panel2(j).Width,9%y)
     BTN2(j).TextSize=20
     BTN2(j).Text="Group "&j
     BTN2(j).Background=cdGroepsknop
     
     Signal(j).Initialize("Signal")
     Panel2(j).AddView(Signal(j),0.075*Panel2(j).Width,BTN2(j).top + 0.25*BTN2(j).height,0.5*BTN2(j).height,0.5*BTN2(j).height)
     Signal(j).Gravity=Gravity.FILL
     Signal(j).Bitmap=cdButtongroen

Dim t As GroepButtonTags
     t.index=j
     BTN2(j).Tag=t

     For i=1 To 15
         BTN1(j,i).Initialize(Me,"BTN1",0.1*Panel2(j).Width,(i)*(10%y)+1%y,0.85*Panel2(j).Width,9%y)
       Panel2(j).AddView(BTN1(j,i).AsView,0.1*Panel2(j).Width,(i)*(10%y)+1%y,0.85*Panel2(j).Width,9%y)  
       
       BTN1(j,i).index=(j*16)+i
       BTN1(j,i).Group=j
       BTN1(j,i).Unit=i
       
     Next
     
   Next
End Sub

Now, what this does is place the button(BTN2) on top of the Signallight. Tried everything, and it used to work. BringToFront or SendToBack even don't seem to do anything.

Any clues?
 

stanmiller

Active Member
Licensed User
Longtime User
Upload a zip of the sample project. Then anyone can load it up in the IDE, take a look, and provide feedback.

1_export_as_zip_zpsogclbxnk.jpg
 
Upvote 0

Blacky's Boss

Member
Licensed User
Longtime User
Allright, Here you go, but I warn you, it is not as 'clean' as it should be....
 

Attachments

  • HomeImprovement.zip
    306.2 KB · Views: 212
Upvote 0

stanmiller

Active Member
Licensed User
Longtime User
Allright, Here you go, but I warn you, it is not as 'clean' as it should be....

No worries. "Meaningful invention is a messy process..."

What is the expected behavior? Tap on a button and it shows the yellow icon? Do the buttons reflect the current status of the assigned hardware at startup? (e.g. if the device is on, then the yellow icon would be on top? Otherwise the group button?

This?

2_bb_buttons_zpsqv85dgjx.jpg


Or this?

1_bb_lights_zps7yyrqixt.jpg
 
Upvote 0

Blacky's Boss

Member
Licensed User
Longtime User
Neither of those. The Icon should always be on top of the button, and shows the status of the group. Tap the Groupbutton to expand/collapse the grey panel, showing the 15 unitbuttons. If all the units are off, then the Icon on the Groupbuttun is Red, if one or more of the units are on, then the icon is orange, and if they are all on, the icon is green(ish,:D).

Long-pressing a Groupbutton switches a whole group on/off, and offcourse pressing a unitbutton switches a unit on/off. If the unit is a dimmer, the unitbutton shows the dimmericon, and the user is should be able to slide a Lightbulb icon left and right on the button. But I'm still working on the dimmer part.

The unitbuttons have the same problem, there should be a Lightbulb and a Dimmer icon on there.
And it did work, but somehow I managed to #$%$ it up, an now all the Icons are behind the buttons.:confused:
 
Last edited:
Upvote 0

Blacky's Boss

Member
Licensed User
Longtime User
BringToFront doesn't seem to do anything.

Putting it next to the button... No. That's avoiding the problem, not solving it.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
The Icon should always be on top of the button
If you are using android 5+ then you need to change the buttons elevation.

unitbutton:
B4X:
Sub SetElevation(v As View, e As Float)
    Dim jo As JavaObject
    Dim p As Phone
  
    If p.SdkVersion >= 21 Then
        jo = v
        jo.RunMethod("setElevation", Array As Object(e))
    End If
End Sub

and then set the buttons elevation

B4X:
    Knop.Initialize("Knop")
    Hbase.AddView(Knop,0,0,W,H)
    Knop.TextSize=20
    Knop.SetBackgroundImage(Bitmaps.Achtergrond)
    Knop.TextColor=Colors.White
    Knop.Gravity = Bit.Or(Gravity.CENTER_HORIZONTAL, Gravity.CENTER_VERTICAL)
    SetElevation(Knop,0)
    cover.Initialize("cover")
    Hbase.AddView(cover,0,0,W-D,H)

results on my device running Android 6.

Screenshot_20160717-101949_Bildgröße ändern.png
 
Upvote 0

Blacky's Boss

Member
Licensed User
Longtime User
Well, thnx so far guys, but It still does not work overhere. I literally copied DonManfreds solution, and it just doesn't.... o_OI don't get it...o_O
 
Upvote 0

Blacky's Boss

Member
Licensed User
Longtime User
:DHmmm.. If there is no settingsfile, or there are no systems in the file, you are redirected to the settings page. As you might suspect, I'm a little reluctant to give you the data of the only working system, as it's controlling my domestic lights, catflap and the pump in the gardenpond:D

So if you want to give it a go, then please -try to- disable the redirect. I'm not at home right now to do it myself....
 
Upvote 0

stanmiller

Active Member
Licensed User
Longtime User
While some diagnosis can be done with a glance of code, it's easier with an example we can work with. As noted, your project was missing a settings file. I worked around by commenting out 'StartActivity(Settings). Your project also uses automation to create the views adding math to the mix.

The misbehavior is a UX matter. The nested loops and offsets and positioning calculations muddy the picture.

In situations like these it's often easier to create a simple prototype in the Designer, model the desired behavior with a few layered icons and buttons and panels, and sort things out. Then set the layout aside and automate as you've done using code to replicate the behavior you set forth in the designer.

We can jump in and help with the prototype layout if you get stuck. And I expect this could be done in your existing project. Just create a new activity for your modeling work inside the project.

Also checkout this post from Klaus. It shows a clever way to load a layout into a ScrollView. Magic! This way you could keep your current architecture but temporarily substitute a designer layout for the automated view creation and attachment.

ScrollView example with a Panel higher than the screen
https://www.b4x.com/android/forum/t...-panel-higher-than-the-screen.9539/#post52833

3_bb_klaus_scroll_view_zpspwekredm.jpg
 
Last edited:
Upvote 0

Blacky's Boss

Member
Licensed User
Longtime User
Allright, I picked this up again. I simply opened a new layout with a button, a checkbox and and an imageview. the abstract designer shows the right Zorder, but the WYSIWYG designer on my phone always places the button on top, whatever I try. I can't believe I am the only one experiencing this?

To fix the z order issue, where buttons appear above all other views, you need to set the front panel elevation to 2dip or more.
Well, then I'm either really blind:cool:, or really stupid:confused:, but I don't see how....:eek:
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Since you have a bunch of similar buttons, why not create a custom view? That way it would be a lot easier to work with, and it would be easily re-usable in other projects
 
Upvote 0

Blacky's Boss

Member
Licensed User
Longtime User
@ DonManFred, well, in my latest test, there is no panel. No scrollview. Just a new activity with a new button and a new imageview. As simple as that. Whatever I do, the button is allways displayed on top of all other views.

@ Cableguy, I'm in the process of making a class for the button. There will be no other project to use these in.
 
Upvote 0
Top