Android Question How to programm modal input window?

welu1805

Active Member
Licensed User
Longtime User
Hi all,

on my main activity I want to press a button, than should come a window with input fields (edittext). This window should be modal. How can I programm this on principle?

Thanks
Lutz
 

JonPM

Well-Known Member
Licensed User
Longtime User
What help do you need exactly? Making the popup modal? Place a panel behind it with a click sub and return true
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
Thank you JonPM,

that was my problem. How to catch the click event of the panel. Now it works. Return True is not nessecary because pnlScreen_Click has no ReturnValue.

One small problem is still: If I press the Back-key I expect that only the modal dialog will be closed but the app closes.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You need to manage this in the Activity_KeyPress event, something like this:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
    If KeyCode = KeyCodes.KEYCODE_BACK
        If PanelX.Visible = True Then
            PanelX.Visible = False
            Return True
        Else
            Return False
        End If
    End If
End Sub
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
I want to create an own class for the dialog. Is it possible to trap the Activity_KeyPress event within this class?
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
Hi all, best wishes for the new your!

Now I tested the library "DialogView". If I call a simple modal dialog with the folloing code

Dialog.LoadLayout("dialog1").ShowYesNo("Login", "yes", "no")

a modal dialog window appears. After pressing the back key THIS DIAOLG is closed and not the app. Because it runs in debug mode I can see that there is no new activity. Therefore it MUST be possible to trap the back key in the Dialog class in the library.

How does it work?
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
I found a solution which also works within a class:

In the class is a panel for the background of the dialog called "pnlBackground"
Further there is a Sub "Show" which sets pnlBackground.Visible = True.

I added this code after "pnlBackground.Visible = True":

Dim r As Reflector
r.Target = pnlBackground
r.SetOnKeyListener("OnKeyPress")
r.RunMethod2("setFocusable", "True", "java.lang.boolean")
r.RunMethod2("setFocusableInTouchMode", "True", "java.lang.boolean")
pnlBackground.RequestFocus

This is the Sub "OnKeyPress":

Sub OnKeyPress(ViewTag As Object, KeyCode As Int, KeyEvent As Object) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
If pnlBackground.Visible Then
pnlBackground.Visible = False
Return(True)
Else
Return(False)
End If
Else
Return(False)
End If
End Sub

Now the back key closes only the dialog.
 
Upvote 0

welu1805

Active Member
Licensed User
Longtime User
This is #14 again with formatted code:

I found a solution which also works within a class:

In the class is a panel for the background of the dialog called "pnlBackground"
Further there is a Sub "Show" which sets pnlBackground.Visible = True.

I added this code after "pnlBackground.Visible = True":

B4X:
Dim r As Reflector
r.Target = pnlBackground
r.SetOnKeyListener("OnKeyPress")
r.RunMethod2("setFocusable", "True", "java.lang.boolean")
r.RunMethod2("setFocusableInTouchMode", "True", "java.lang.boolean")
pnlBackground.RequestFocus

This is the Sub "OnKeyPress":

B4X:
Sub OnKeyPress(ViewTag As Object, KeyCode As Int, KeyEvent As Object) As Boolean
  If KeyCode = KeyCodes.KEYCODE_BACK Then
    If pnlBackground.Visible Then
      pnlBackground.Visible = False
      Return(True)
    Else
      Return(False)
    End If
  Else
    Return(False)
  End If
End Sub

Now the back key closes only the dialog.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…