Android Tutorial Handle the soft keyboard with the IME library

Status
Not open for further replies.
Android has very good support for custom input method editors (IMEs).
The downside for this powerful feature is that interacting with the soft keyboard can be sometimes quite complicated.

This library includes several utilities that will help you better handle the soft keyboard.

The attached example demonstrates the available methods.

1724752241705.png


Note that the IME object should be initialized before it can be used.

Handling the screen size changed event
When the keyboard opens the available screen size becomes much shorter. By default if the EditText is located near the bottom of the screen, Android will "push" the whole activity and make the EditText visible. This mode is named "adjustPan" mode.

By calling IME.AddHeightChangedEvent you are changing the activity to "adjustSize" mode. In this mode the activity will not be pushed automatically. Instead the HeightChanged event will be raised when the keyboard is shown or hidden.

Update: You should explicitly set the adjustSize mode with the manifest editor. This is done by adding the following manifest editor code (for each activity):
B4X:
SetActivityAttribute(main, android:windowSoftInputMode, adjustResize|stateHidden)

For example the following code makes sure that the button at the bottom is always visible and sets the large EditText height to match the available height:
B4X:
Sub IME_HeightChanged(NewHeight As Int, OldHeight As Int)
   btnHideKeyboard.Top = NewHeight - btnHideKeyboard.Height
   EditText1.Height = btnHideKeyboard.Top - EditText1.Top
End Sub

The result is:

1724752272411.png


Note that this method will not work if the activity is in full screen mode (Issue 5497 - android - adjustResize windowSoftInputMode breaks when activity is fullscreen - Android).

Showing and hiding the keyboard
IME.ShowKeyboard - Sets the focus to the given view and opens the soft keyboard.
IME.HideKeyboard - Hides the keyboard (this method is the same as Phone.HideKeyboard).

Handle the action button
By calling IME.AddHandleActionEvent you can override the default behavior of the action button (the button that shows Next or Done).
This event is similar to EditText_EnterPressed event. However it is more powerful. It also allows you to handle the Next button and also to consume the message (and keep the keyboard open and the focus on the current EditText).

This can be useful in several cases.
For example in a chat application you can send the message when the user presses on the done button and keep the keyboard open by consuming the message.

You can also use it to validate the input before jumping to the next view by pressing on the Next button (note that the user will still be able to manually move to the next field).

You can use the Sender keyword to get the EditText that raised the event.
For example:
B4X:
Sub IME_HandleAction As Boolean
   Dim e As EditText
   e = Sender
   If e.Text.StartsWith("a") = False Then
      ToastMessageShow("Text must start with 'a'.", True)
      'Consume the event.
      'The keyboard will not be closed
      Return True
   Else
      Return False 'will close the keyboard
   End If
End Sub

Custom filters
EditText.InputType allows you to set the keyboard mode and the allowed input.
However there are situations where you need to use a custom filter. For example if you want to accept IP addresses (ex: 192.168.0.1). In this case none of the built-in types will work. Setting the input type to INPUT_TYPE_DECIMAL_NUMBERS will get you close but it will not allow the user to write more than a single dot.
IME.SetCustomFilter allows you to both set the keyboard mode and also to set the accepted characters.
In this case we will need a code such as:
B4X:
IME.SetCustomFilter(EditText3, EditText3.INPUT_TYPE_NUMBERS, "0123456789.")
Note that this is only a simple filter. It will accept the following input (which is not a valid IP address):
....9999.

Updates:

The example was updated and is now based on B4XPages. Note that additional code in the Main module (https://www.b4x.com/android/forum/t...or-managing-multiple-pages.118901/post-745090) and don't miss the manifest editor code.
 

Attachments

  • IME_Example.zip
    14.8 KB · Views: 161
Last edited:

MaxApps

Active Member
Licensed User
Longtime User
I cant get" IME1.AddHeightChangedEvent" to work.

In my program I have the following:

Sub globals
Dim IME1 As IME



Sub Activity_Create(FirstTime As Boolean)
IME1.Initialize("IME1")
IME1.AddHeightChangedEvent


Sub IME1_HeightChanged(NewHeight As Int, OldHeight As Int)
edtMain.Height = NewHeight - (edtMain.Top+10%y)
End Sub

It work 3 out of 5 time, but the 2 ot of 5 time, it does not do the command in Sub IME1_Hei....

Is something missing?

Kind regards
Jakob
 

MaxApps

Active Member
Licensed User
Longtime User
Hi Erel

Can I send you the complete code, to privately? It is one of my full/published apps, that I am improving and really need the height changed part of IME.

It will not be easy, to shave it down the only show the problem.

Kind regards
Jakob
 

luke2012

Well-Known Member
Licensed User
Longtime User
Currency filter ?

Hi to all,
there is a way to implement a currency filter / mask ?

I mean that user can input only the currency format like : X,00 (with 2 decimals mask)
 

Askjerry

Member
Licensed User
Longtime User
Okay... something is not working right...

After reading a bunch of forum posts I understand that I should be using IME to close the keyboard. Digging through your code there is a command...

IME.HideKeyboard that should hide the keyboard... but I can't seem to get there.


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

Dim soundeffect As MediaPlayer
Dim IME As IME
End Sub

...and I get...

Compiling code. Error
Error parsing program.
Error description: Unknown type: ime
Are you missing a library reference?
Occurred on line: 6
Dim IME As IME

... and I restarted the basic4android IDE... no change.

When I look in the Library folder... the ime.jar and ime.xml files are there. Thoughts?

Thanks,
Jerry :)
 
Last edited:

Askjerry

Member
Licensed User
Longtime User
Ahhhhhhhhh!

That did it. :icon_clap:

I've been programming BASIC since 1978... but not this BASIC. That will answer a lot of other questions on things like the TTS, GPS, etc. Now I see that they need to be enabled in the tab when needed... makes sense.

So now that I understand that... I'll start reading the references for other applications.
:wav:
THANK YOU!

Jerry
 

Bashar

New Member
Licensed User
Longtime User
The state of the keyboard when the activity appears is set in the manifest file.
You should use this code (manifest editor):
B4X:
SetActivityAttribute(main, android:windowSoftInputMode, stateAlwaysVisible)
Hi
The activity starts with an alpha animation (2 sec) and only then do I want to display the keyboard. With the manifest entry, I get the keyboard immediately, which ruins the animation.

Possible?

Thanks
 

sorex

Expert
Licensed User
Longtime User
BTW, if you set EditText.ForceDone = True (or with the designer) then the keyboard will be hidden automatically when the user presses on the done button.

indeed, but I have a rather large form here (13 elements or so) and this

select element > typr > done > select other element > type > done > select other element

will be very annoying compared to the NEXT button.


I have put this form in a scrollview that is about 80-90 of the activity height.

What code should I use in the IME_HeightChanged to make my edit box visible when advancing with the NEXT button? (I prefer the NEXT feature but you don't have a clue what you're editing when the keyboard covers it)
 

sorex

Expert
Licensed User
Longtime User
This issue got solved.

TDS gave me the right code to add to the manifest file.
 

moster67

Expert
Licensed User
Longtime User
Search button instead of Enter-button

There are apps which when touching the EditText, brings up the on-screen keyboard and the keyboard now has a "Search" button instead of an Enter key.

Is this possible using the IME-library?
 

deantangNYP

Active Member
Licensed User
Longtime User
IME.ShowKeyboard

Is it possible to permanent display the Keyboard (IME.ShowKeyboard) at all times regardless of the focus of the cursor, unless IME.HideKeyboard is called?
tks
 
Status
Not open for further replies.
Top