Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

Handle the soft keyboard with the IME library

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-09-2012, 12:38 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default Handle the soft keyboard with the IME library

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.



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.
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:
Code:
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:



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 opened 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:
Code:
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:
Code:
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):
Quote:
....9999.
The example is attached.

The library is available here: http://www.basic4ppc.com/forum/addit...html#post84114
Attached Files
File Type: zip IME.zip (10.5 KB, 330 views)
Reply With Quote
  #2 (permalink)  
Old 02-09-2012, 12:44 PM
Basic4ppc Expert
 
Join Date: Jan 2011
Location: Swindon UK
Posts: 566
Default

Nice one Erel, more control is always better.

Thanks
__________________
Living proof that a little knowledge can be a dangerous thing!
Reply With Quote
  #3 (permalink)  
Old 02-09-2012, 01:02 PM
Junior Member
 
Join Date: Dec 2011
Location: Fortaleza, Brazil
Posts: 15
Default

Thank you! Day after day is best to work with this wonderful IDE.
__________________
Rildo Moraes
streamsolucoes.com
Reply With Quote
  #4 (permalink)  
Old 02-25-2012, 02:05 PM
Basic4ppc Veteran
 
Join Date: Feb 2011
Location: Chicago area (NW Indiana, USA)
Posts: 325
Default

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?
Attached Images
File Type: jpg IME_issue.jpg (21.3 KB, 75 views)
Reply With Quote
  #5 (permalink)  
Old 02-26-2012, 06:42 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

It seems like a bug in Android.
Reply With Quote
  #6 (permalink)  
Old 02-28-2012, 10:07 PM
JesseW's Avatar
Basic4ppc Veteran
 
Join Date: Oct 2008
Posts: 250
Default

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.
Reply With Quote
  #7 (permalink)  
Old 03-11-2012, 12:49 PM
Basic4ppc Expert
 
Join Date: May 2008
Location: Italy
Posts: 599
Awards Showcase
Beta Tester 
Total Awards: 1
Default

I implemented the IP-address filter as in the example-file i.e.

Code:
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.
__________________
rgds,
moster67
Reply With Quote
  #8 (permalink)  
Old 03-12-2012, 04:57 AM
Basic4ppc Veteran
 
Join Date: Feb 2011
Location: Chicago area (NW Indiana, USA)
Posts: 325
Default

Quote:
Originally Posted by moster67 View Post
I implemented the IP-address filter as in the example-file i.e.

Code:
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.

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.
Reply With Quote
  #9 (permalink)  
Old 03-17-2012, 11:45 PM
Basic4ppc Expert
 
Join Date: May 2008
Location: Italy
Posts: 599
Awards Showcase
Beta Tester 
Total Awards: 1
Default

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)

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.

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.


Quote:
Originally Posted by Kevin View Post
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.

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.
__________________
rgds,
moster67

Last edited by moster67 : 03-17-2012 at 11:59 PM.
Reply With Quote
  #10 (permalink)  
Old 03-27-2012, 02:21 PM
Basic4ppc Veteran
 
Join Date: Feb 2011
Location: Chicago area (NW Indiana, USA)
Posts: 325
Default

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....
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Intercept back button from soft keyboard capisx Basic4android Updates and Questions 6 01-30-2012 09:19 AM
Soft keyboard question Ricky D Basic4android Updates and Questions 2 01-12-2012 10:21 PM
Soft keyboard question Ricky D Basic4android Updates and Questions 1 01-12-2012 07:37 AM
Smart soft keyboard. nm7s9 Basic4ppc Wishlist 0 06-23-2010 12:03 PM
handle program with voice Georg Questions (Windows Mobile) 1 04-11-2008 04:42 AM


All times are GMT. The time now is 10:10 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0