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.

SS-2012-02-09_14.42.47.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:

SS-2012-02-09_14.49.28.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.

The example is attached.

The library is available here: http://www.b4x.com/forum/additional...4834-ime-library-soft-keyboard.html#post84114
 

Attachments

  • IME.zip
    11.3 KB · Views: 7,918
Last edited:

MrKim

Well-Known Member
Licensed User
Longtime User
I am having an issue with ShowKeyboard that I hope someone can help with.
It pushes the screen up just fine IF the EditText box does NOT ALREADY have the focus.
B4X:
IME1.ShowKeyboard(ActTodayTB)  'works if ActTodayTB does not have the focus when called
If the edit text box already has the focus, it opens the keyboard but does not 'push' the screen up and my EditText is hidden.

I tried setting the focus to another EditText first but it made no difference.
B4X:
SetupHrs.RequestFocus
IME1.ShowKeyboard(ActTodayTB)

To be clear, the keyboard always opens just fine, the issue is it does not push the screen if the desired EditText already has the focus.

EDIT:
Nothing I do in code seems t resolve this. The only time the screen is pushed is if the LAST EditText called in code with ShowKeyboard does NOT have the focus. For example:

B4X:
IME1.ShowKeyboard(NewCycleTime)
DoEvents
IME1.ShowKeyboard(ActTodayTB)
does not work either IF ActTodayTB already has the Focus.
I even tried setting the focus to a different EditText in another event so that user input would be required before the ShowKeyboard command - it did not work either. The only way I can find that will cause the screen Push is if the Focus is changed to a different EditText manually on the screen.

Thanks for any help.
 
Last edited:

MrKim

Well-Known Member
Licensed User
Longtime User
Try to explicitly set the adjust mode to adjustPan:
B4X:
SetActivityAttribute(workadd, android:windowSoftInputMode, "stateHidden|adjustPan")
If it will not help (which will probably be the case) then you will need to switch to adjustResize and manually handle the layout with the HeightChanged event.
Thanks, Erel. I will give that a try. If it doesn't work - Ugh.
 

MrKim

Well-Known Member
Licensed User
Longtime User
Try to explicitly set the adjust mode to adjustPan:
B4X:
SetActivityAttribute(workadd, android:windowSoftInputMode, "stateHidden|adjustPan")
If it will not help (which will probably be the case) then you will need to switch to adjustResize and manually handle the layout with the HeightChanged event.
Hate to bother you again but SetActivityAttribute doesn't compile and a search for it doesn't turn up anything relevant. Am I missing a lib?
 

MrKim

Well-Known Member
Licensed User
Longtime User
Are you adding that line to the Manifest?

Added it to the manifest and compiler throws error: "Module: workadd not found."

Below is the complete manifest.

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'Kim - added next line for the sole purpose of geting a black cursor - also changes the defaul color of all text from white to black
SetApplicationAttribute(android:theme, "@android:style/Theme.Holo.Light")
'added next line to attempt to fix push problem
SetActivityAttribute(workadd, android:windowSoftInputMode, "stateHidden|adjustPan")
'End of default text.

Thanks for any help.

Kim
 

appie21

Active Member
Licensed User
Longtime User
How can I set a event if the Done key is pressed on the standard keyboard

(Can't find any topic about this)
 

Prosg

Active Member
Licensed User
Longtime User
Hello,

I use

B4X:
 oIME.showKeyboard(txtPointsForts)

to show the keyboard for my editText (no singleline) but i don't know how to add the "Return" key in the keyboard ?

ty
 

ViMeAv ICT

Member
Licensed User
Longtime User
I need a possibility to fill in numeric and alpha at the same time, not the phone keyboard.
(because then I have to hit the button three times for the z char)
Is this possible?
 

Cebuvi

Active Member
Licensed User
Longtime User
Hello,

Is possible to push a panel with a custom list view?
I'm trying:
B4X:
Sub IME1_HeightChanged(NewHeight As Int, OldHeight As Int)
  
   pnlLista.Height=30%y
End Sub


but does not work. It's not possible scroll the custom list view.

Thanks.

Cesar
 

Ju Yang

Active Member
Licensed User
Longtime User
I want to change Android's own keyboard on the text, how should I do?
 
Status
Not open for further replies.
Top