Centering an EditText view in a panel.

keirS

Well-Known Member
Licensed User
Longtime User
I am trying center an EditText view in a panel that should mean there is a 5 pixel red border around the edittext. I am obviously missing something fundamental (noob with android) as I would expect the code below to work. What am I doing wrong? Is the border of the EditText not included in it's height and width?



B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
    Dim EditBoxPanel As Panel
    Dim ET1 As EditText
    Dim CDB As ColorDrawable
    Dim EditBoxPanel2 As Panel
    Dim ET2 As EditText
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   CDB.Initialize(Colors.ARGB(255,255,0,0),0)
    EditBoxPanel.Initialize("")
   ET1.Initialize("")
   EditBoxPanel.Initialize("")
   ET2.Initialize("")
   EditBoxPanel2.Initialize("")
   ET1.TextSize  = 10
   ET2.TextSize = 10
   Activity.Height = 100%y
   Activity.Width = 100%x
   
   
   
   Activity.AddView(EditBoxPanel,5,100,100,40)
   EditBoxPanel.AddView(ET1,5,5,90,30)
   EditBoxPanel.Background = CDB
 
   EditBoxPanel2.Background = CDB
   Activity.AddView(EditBoxPanel2,5dip,5dip,100dip,40dip)
   EditBoxPanel2.AddView(ET2,5dip,5dip,90dip,30dip)
 End Sub
 

NJDude

Expert
Licensed User
Longtime User
Try this:
B4X:
Sub Activity_Create(FirstTime As Boolean)

    CDB.Initialize(Colors.ARGB(255, 255, 0, 0), 0)

    EditBoxPanel.Initialize("")
    ET1.Initialize("")
    EditBoxPanel.Initialize("")
    ET2.Initialize("")
    EditBoxPanel2.Initialize("")
    ET1.TextSize = 10
    ET2.TextSize = 10

    Activity.Height = 100%y
    Activity.Width = 100%x

    Activity.AddView(EditBoxPanel, (100%x - 100dip) / 2, 100dip, 100dip, 40dip)
    EditBoxPanel.AddView(ET1, 5dip, 5dip, 90dip, 30dip)
    EditBoxPanel.Background = CDB
    EditBoxPanel2.Background = CDB
    
    Activity.AddView(EditBoxPanel2, (100%x - 100dip) / 2, 5dip, 100dip, 40dip)
    EditBoxPanel2.AddView(ET2, 5dip, 5dip, 90dip, 30dip)

 End Sub
 
Upvote 0

keirS

Well-Known Member
Licensed User
Longtime User
Sorry you misunderstood what I am after. The image shows what I am getting.
I want the edittext to be centered within the red panel.
MBpbS2Cw.png
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
How about now?
B4X:
Activity.AddView(EditBoxPanel, (100%x - 100dip) / 2, 100dip, 100dip, 40dip)
EditBoxPanel.AddView(ET1, (EditBoxPanel.Width - 90dip) / 2, (EditBoxPanel.Height - 25dip) / 2, 90dip, 30dip)
EditBoxPanel.Background = CDB
EditBoxPanel2.Background = CDB
Activity.AddView(EditBoxPanel2, (100%x - 100dip) / 2, 5dip, 100dip, 40dip)
EditBoxPanel2.AddView(ET2, (EditBoxPanel.Width - 90dip) / 2, (EditBoxPanel.Height - 25dip) / 2, 90dip, 30dip)
 
Upvote 0
Top