B4A Library Custom Input Box - Editbox code module

The Android system's inputbox dialog is useful, but it can leave the keyboard open when done. This can be an annoyance to users. To solve this, I have created a custom code module that can be used instead of the built in inputbox dialog.

First, add the EditBox code module to your project, and add the phone library that allows the EditBox code module to remove the keyboard if it remains open.

Next, we'll declare the needed variables and set up a basic activity screen.

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim eb As cInputBox
   Dim EditShowBtn As Button
   Dim Label2 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
   Activity.SetBackgroundImage(LoadBitmap(File.DirAssets,"blueback.jpg"))
End Sub

Finally, we'll add some code to show the cInputDialog and handle events.

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'Return True to consume the event
   If KeyCode=KeyCodes.KEYCODE_BACK AND eb.Visible=True Then 
      EditBox.Hide(eb)
      Return True
   End If
End Sub

Sub EditShowBtn_Click
   Dim r As Int
   r=EditBox.Show(eb,Activity,"Enter some text in the box below.","Enter text here","OK","Cancel",eb.cInputBoxEditText.INPUT_TYPE_TEXT)
   If r=DialogResponse.POSITIVE Then Label2.Text=eb.Result
End Sub

'Edit box events

Sub cInputBoxBtn_click
   Dim b As Button
   b=Sender
   EditBox.Button_click(b.Tag,eb,Activity)
End Sub

Note: This code module mimics the behavior of a modal dialog. Pressing the back button in the demo removes the dialog.
 

Attachments

  • screenshot.jpg
    screenshot.jpg
    7.9 KB · Views: 2,213
  • editbox.zip
    9.8 KB · Views: 962
Last edited:

techknight

Well-Known Member
Licensed User
Longtime User
Problem:

eb.cInputBoxEditText.InputType = eb.cInputBoxEditText.INPUT_TYPE_NUMBERS

fails. any ideas?
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
TechKnight,

I updated the module so you can set the input type from the Show routine. Download it again. You can use the following to set the type to numbers.

B4X:
r=EditBox.Show(eb,Activity,"Enter some text in the box below.","Enter text here","OK","Cancel",eb.cInputBoxEditText.INPUT_TYPE_NUMBERS)
 

slugger

Member
Licensed User
Longtime User
Hi,

I am using your code module with some slight color modifications, thanks for sharing it. :)

I use it instead of the usual modal dialogs.

I have one problem though.

With the regular modal dialogs when I click the OK button the dialog closes and I get back to the views that were under the dialog, and I find them exactly like I left them.

If I use your module, when I click the OK button (the same goes with the CANCEL button) the input box closes and I find that the first EditText on the underlying panel receives focus and the FocusChanged event is triggered.

Any ideas on why your module works like that and how to avoid it?

Thank you in advance.
 

luciano deri

Active Member
Licensed User
Longtime User
Hi, i include your class module in my project but don't pass by cInputBoxBtn_click. Why ?
 

techknight

Well-Known Member
Licensed User
Longtime User
I had run into an odd bug. Every great, and I mean GREAT once in awhile, the editbox will lock up the user input ability of the app. Almost as if the editbox randomly looses focus or something. I cant click OK, cancel, or select the editbox in any way.

The app is still running its timers, and doing stuff in the background, but the editbox panel "freezes" itself.
 

techknight

Well-Known Member
Licensed User
Longtime User
No, at least I am not drawing anything on that. The thing that makes this hard, is its intermittent. Personally I cant get it to screw up, everything works fine. But certain times of the day, certain planet alignments, cosmic radiation, or solar flares it will lock up. App is still running in the background but the editbox just locks up on screen. cant edit, cant remove the keyboard away, cant click ok or cancel. I also cant forcefully trigger it, like tapping anywhere else, pressing home button and switching back etc... Hardest to troubleshoot.

Really strange... Incase it helps, I am using Tab 4, and API 14+ enforcement in the manifest. What I am not aware of, if the target device is running kitkat, or lollipop. Seems the Tab 4 is divided in terms of what OS its running on i have noticed.
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
That is very strange. I wish I could help, but those intermittent problems are awfully hard to diagnose!

You could try the input dialog of the dialogs library to see if that does better.
 

techknight

Well-Known Member
Licensed User
Longtime User
it modal. Stops the clocks running in the background which isnt going to work.
 

Jack Cole

Well-Known Member
Licensed User
Longtime User
I have a theory. It may have to do with the app life cycle. For example, user gets a phone call and resumes in the app after (when the box is open). Or goes to home screen then back into the app (when it is still running). If there is content being added when activity_create (firsttime=false) or in activity_resume, it may be added on top of the edit box.
 

techknight

Well-Known Member
Licensed User
Longtime User
I figured it out. The EditBox panel can be clicked through. Seems people were fat-fingering the app and freezing the app because the way my code was laid out when the editbox is open.

thanks.
 

techknight

Well-Known Member
Licensed User
Longtime User
I figured out the exact issue.

If I press a button to bring up the EditBox, and I "click through" the editbox panel, and hit that same button again, it brings up a "brighter" editbox and never goes away. its stuck there indefinitely.

Same thing if I hit the same button again if its outside of the edit box. If I call an editbox using the same subroutine that called the editbox before it, it gets stuck.
 

techknight

Well-Known Member
Licensed User
Longtime User
I added a simple GotFocus boolean. if there is an editbox already showing, dont show another one. just return.
 
Top