Android Code Snippet Non-centered Custom layout dialog


Staff member
Licensed User

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:

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.

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)
   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.