1. Download the free trial version

Tutorial Handle the soft keyboard with the IME library

Discussion in 'Tutorials & Examples' started by Erel, Feb 9, 2012.

  1. Erel

    Erel Administrator Staff Member Licensed User

    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.

    [​IMG]

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

    [​IMG]

    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):
    The example is attached.

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

    Attached Files:

    • IME.zip
      File size:
      11.3 KB
      Views:
      1,189
  2. stevel05

    stevel05 Expert Licensed User

    Nice one Erel, more control is always better.

    Thanks
     
  3. rildomoraes

    rildomoraes Member Licensed User

    Thank you! Day after day is best to work with this wonderful IDE.
     
    supfree likes this.
  4. Kevin

    Kevin Well-Known Member Licensed 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?
     

    Attached Files:

  5. Erel

    Erel Administrator Staff Member Licensed User

    It seems like a bug in Android.
     
  6. JesseW

    JesseW Active Member Licensed 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.
     
    thuryanto likes this.
  7. moster67

    moster67 Well-Known Member Licensed User

    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.
     
  8. Kevin

    Kevin Well-Known Member Licensed User


    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.
     
  9. moster67

    moster67 Well-Known Member Licensed 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.


     
    Last edited: Mar 17, 2012
  10. Kevin

    Kevin Well-Known Member Licensed 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....
     
  11. margret

    margret Well-Known Member Licensed 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.
     
  12. Kevin

    Kevin Well-Known Member Licensed User


    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:
     
  13. Kevin

    Kevin Well-Known Member Licensed User


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

    Code:
    txtNewChNum.InputType = txtNewChNum.INPUT_TYPE_DECIMAL_NUMBERS
     
  14. rgately

    rgately Member Licensed User

    Thank you for adding the ability to programatically show the keyboard and the other useful features.
     
  15. Dirk A

    Dirk A Member Licensed 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

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

    Erel Administrator Staff Member Licensed User

    Can you upload your project (or a smaller one that reproduces this issue)?
     
  17. Dirk A

    Dirk A Member Licensed 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...
     
  18. Dirk A

    Dirk A Member Licensed 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
     

    Attached Files:

  19. Erel

    Erel Administrator Staff Member Licensed User

    Which device are you using?
    I tested your program and I do see the 4 views (without commenting this line).
     
  20. Dirk A

    Dirk A Member Licensed User

    samsung gt-p6200
     

Share This Page

Loading...