Android Code Snippet Non-centered Custom layout dialog

SS-2017-10-26_08.55.31.png


This example uses JavaObject to change the CustomLayoutDialog position.
It also demonstrates how to access the layout fields and how to make sure that the keyboard is closed when the dialog is dismissed.

The layout file looks like this:

SS-2017-10-26_08.56.28.png


Note that EditText1 is the first view and EditText2 is the second view. You can change the order of views by dragging them in the views tree.

Code:
B4X:
Sub Activity_Click
   Dim dialog As CustomLayoutDialog
   Dim sf As Object = dialog.ShowAsync("", "Yes", "Cancel", "No", Null, False)
   dialog.SetSize(100%x, 300dip)
  'Code to change top position
   Dim jo As JavaObject = sf
   Dim window As JavaObject = jo.RunMethodJO("getWindow", Null)
   Dim lp As JavaObject = window.RunMethod("getAttributes", Null)
   lp.SetField("gravity", Bit.Or(Gravity.TOP, Gravity.CENTER_HORIZONTAL))
   lp.SetField("y", 40dip) 'set the top position
   window.RunMethod("setAttributes", Array(lp))
   '****
   Wait For (sf) Dialog_Ready (DialogPanel As Panel)
  
   DialogPanel.LoadLayout("DialogLayout")
   Dim firstName As EditText = DialogPanel.GetView(0)
   Dim lastname As EditText = DialogPanel.GetView(1)
   Wait For (sf) Dialog_Result (Result As Int)
   firstName.Enabled = False 'disable the text fields to make the keyboard disappear
   lastname.Enabled = False
   If Result = DialogResponse.POSITIVE Then
     Log("First name: " & firstName.Text)
     Log("Last name: " & lastname.Text)
   End If
End Sub

Depends on the latest version of Dialogs library.
 
Top