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:

stevel05

Expert
Licensed User
Longtime User
Nice one Erel, more control is always better.

Thanks
 

Kevin

Well-Known Member
Licensed User
Longtime User
Just now getting around to putting this into my project. Thanks!!!

It is working great for IP addresses and the numeric keyboard except for one issue. When I rotate to landscape and there is simply not enough room to show the normal EditText (pushing it would do no good), Android shows a different input box (not the normal EditText). This different box does not show the existing periods. In the attached screenshot, there should be periods in it (as it is an IP address) but they are not there. Oddly enough, I can enter more periods (though it doesn't show them) and if I hide the keyboard or rotate back to portrait then the extra periods I added appear.

To be fair though, after seeing this happen in my app I tried it in a competing app and it has the same problem. For that reason I can accept it if this is the way it must be (could be an Android problem or Swype problem). But I am curious if there is any way to work around it?
 

Attachments

  • IME_issue.jpg
    IME_issue.jpg
    21.3 KB · Views: 3,667

JesseW

Active Member
Licensed User
Longtime User
A little helpful info concerning the HeightChanged event: the NewHeight parameter is the height of the now available part of the screen, not the new height of the keyboard itself.
 

moster67

Expert
Licensed User
Longtime User
I implemented the IP-address filter as in the example-file i.e.

B4X:
IME.SetCustomFilter(EditText3, EditText3.INPUT_TYPE_NUMBERS, "0123456789.")

Now, I have some users (not all) saying that they are unable to use the dot(period). It seems like it happens both with standard keyboards and with third-party keyboards such as Swype. The only thing I noted is that they are Samsung-users (although different models).

The app is running only in portrait-mode so there shouldn't be any landscape-issues as reported by Kevin in a previous post.

So apparently the filter does not work on all devices. For now, I will disable it and rely on the ip-address verification (using regex and for which Erel wrote a great a routine) as the only way to check user-input. It is pity though.
 

Kevin

Well-Known Member
Licensed User
Longtime User
I implemented the IP-address filter as in the example-file i.e.

B4X:
IME.SetCustomFilter(EditText3, EditText3.INPUT_TYPE_NUMBERS, "0123456789.")

Now, I have some users (not all) saying that they are unable to use the dot(period). It seems like it happens both with standard keyboards and with third-party keyboards such as Swype. The only thing I noted is that they are Samsung-users (although different models).

The app is running only in portrait-mode so there shouldn't be any landscape-issues as reported by Kevin in a previous post.

So apparently the filter does not work on all devices. For now, I will disable it and rely on the ip-address verification (using regex and for which Erel wrote a great a routine) as the only way to check user-input. It is pity though.


Hmmm. Interesting find. I've recently lost several thousand installations and I couldn't figure out why. I wonder if it is related. I may have to go back to my custom filtering method I was using before. I haven't seen any comments about this for my app and nobody has emailed me about it, but then the Android user community isn't really known for leaving comments of any useful value. The preferred method seems to be to either simply uninstall or rate 1 star with a useful comment like "didn't work", "sux" or "gay" and then uninstall. :D

There may be another reason (such as something else that I don't know about) but without feedback it is difficult to fix things without knowing they are broken.
 

moster67

Expert
Licensed User
Longtime User
Yep, the feedback is important. I got some negative comments (rightly so) that they couldn't insert the dot in the ip-address field which is somehow essential for my app (it won't work otherwise) :D

Then, of course there were users who were very keen to use my app and they wrote the ip-address somewhere else (where the filter was not implemented) and then copy-pasted it. :D

Anyway, the app in this case is a sort of "community-app" and there is an area opened in a forum so I got most feedback there and avoided many negative rates.


Hmmm. Interesting find. I've recently lost several thousand installations and I couldn't figure out why. I wonder if it is related. I may have to go back to my custom filtering method I was using before. I haven't seen any comments about this for my app and nobody has emailed me about it, but then the Android user community isn't really known for leaving comments of any useful value. The preferred method seems to be to either simply uninstall or rate 1 star with a useful comment like "didn't work", "sux" or "gay" and then uninstall. :D

There may be another reason (such as something else that I don't know about) but without feedback it is difficult to fix things without knowing they are broken.
 
Last edited:

Kevin

Well-Known Member
Licensed User
Longtime User
I got an email from Amazon and they told me that they are unable to enter multiple periods on the Kindle Fire. I don't know how many devices the IME filter doesn't work on but I feel that for whatever reason, it isn't reliable enough to use. Which is a shame because it was nice to have the numeric keyboard pop up for IP entry rather than the full alpha keyboard.

Oh well....
 

margret

Well-Known Member
Licensed User
Longtime User
One app I am writing for the kindle fire uses the numbers keyboard and you can't enter even one period. They have some major issues with their fork of Android. I had to end up using the phone_numbers keyboard, force the next button and reset focus to the first field to even get it to work. It's a nice device but many issues.
 

Kevin

Well-Known Member
Licensed User
Longtime User
One app I am writing for the kindle fire uses the numbers keyboard and you can't enter even one period. They have some major issues with their fork of Android. I had to end up using the phone_numbers keyboard, force the next button and reset focus to the first field to even get it to work. It's a nice device but many issues.


Technically they did tell me that they were "unable to input periods when typing the IP address". I assumed they meant multiple but in light of your post, I guess they literally meant that they could not enter any periods.

I've been putting a lot of work lately into making my app compatible with the Fire. I certainly hope this pays off for me in terms of increased sales on the Amazon market because the device seems to fight you every step of the way.
:sign0080:
 

Kevin

Well-Known Member
Licensed User
Longtime User
One app I am writing for the kindle fire uses the numbers keyboard and you can't enter even one period. They have some major issues with their fork of Android. I had to end up using the phone_numbers keyboard, force the next button and reset focus to the first field to even get it to work. It's a nice device but many issues.


To clarify, are you saying the code below does not work, or do you mean when using the new IME feature in B4A?

B4X:
txtNewChNum.InputType = txtNewChNum.INPUT_TYPE_DECIMAL_NUMBERS
 

rgately

Member
Licensed User
Longtime User
Thank you for adding the ability to programatically show the keyboard and the other useful features.
 

Dirk A

Member
Licensed User
Longtime User
Hey , first of all ,thank you for the usefull library.
When I add
IME1.AddHeightChangedEvent
to my code , my whole layout disappears. Without the line of code everything works fine. i'm working with b4a 1.9

B4X:
Sub Activity_Create(FirstTime As Boolean)
...
IME1.Initialize("IME1")
IME1.AddHeightChangedEvent
IME1.SetCustomFilter(EditDriver, EditChauffeurId.INPUT_TYPE_NUMBERS , "0123456789")
IME1.SetCustomFilter(editCode, editCode.INPUT_TYPE_NUMBERS , "0123456789")
...
end sub    
Sub IME1_HeightChanged(NewHeight As Int, OldHeight As Int)
   buttonLogin.Top = NewHeight - buttonLogin.Height
   
End Sub
 

Dirk A

Member
Licensed User
Longtime User
While trying to "slim down" my project for uploading I have found the origin of the problem .

the edits where i want to use the IME for are located on a panel. In my activity I use a set of panels where I can slide from one to the other (using slidingPanels Lib)

When I put my edits on my main activity i don't have the problem... But that's not an option for me...
 

Dirk A

Member
Licensed User
Longtime User
Attached is the 'Sliding panels demo' where I added the IME and the
AddHeightChangedEvent to illustrate the problem.

When the IME.AddHeightChangedEvent line is present the layout of the first panel does not appear. When you remove the line the layout appears
 

Attachments

  • sliding2.zip
    7.9 KB · Views: 1,223
Status
Not open for further replies.
Top