Android: softkeyboard control on HTC desire
Asked Answered
R

3

5

I wanted a numeric keypad that had a go or done button that closed and executed a calculation class. Thanks to a tip from commonware on where to start I got this working beautifully on the emulator. Then I came to load it on to my HTC desire for testing and it doesn't work at all. I'm sure it must be because of HTC sense having it's own ime but there must surely be a way to make this work on HTC phones? Anyone else managed to get around this issue?

Relativize answered 30/9, 2010 at 21:20 Comment(0)
S
14

I can replicate what I think you are seeing on the HTC Incredible.

Not all soft keyboards will support the IME action button. Some, like the Graffiti soft "keyboard", may have no buttons at all, let alone an IME action button. Even the Compatibility Definition Document says nothing about requiring such an action button for the keyboards supplied with a device.

Hence, you should not rely on the IME action button. If it is there, users can use it. However, always have some other means of accomplishing whatever your goal is.

Sirreverence answered 30/9, 2010 at 22:52 Comment(7)
so aree you saying my best approach is to create my own keyboard, not an IME but a view? I did have a calculate button at one point but it was covered by the keyboard. It's annoying as the action key on the HTC keyboard goes green when it's set to type GO which is great so it understands that much it just returns 0 in the actionid. Im beginning to think creating a little keyboard myself is the way to go but was trying to avoid it? Is that the best approach? Is there any documentation on Android best practice that is acutally readable?Relativize
Oh I've subscribed finally by the way. wish I could afford to attend one of your courses in London, would be quite useful. sadly I'm trying to learn Java and android at the same time by trial and errorRelativize
@Dream Architect: "so aree you saying my best approach is to create my own keyboard, not an IME but a view?" -- for an ordinary application, you'd just have the action button equivalent as part of the activity. For a calculator, you are going to need other "keys" anyway (+, -, *, /, etc.), and so you probably need your own button panel. In that case, I'd put the number keys in that button panel, like a regular calculator has them.Sirreverence
Maybe I'm coming at this the wrong way then. It's not a calculator in that sense. It's a program that requires three numeric inputs to be completed then it executes some specific engineering calculations. The iphone version executes this when the go button is pressed on the keyboard so I was trying to mimic that. I did have a calculate button but it go hidden by the keyboard. I'm sure there is a way around the latter though.Relativize
@Dream Architect: Ah, OK. Well, you can still "mimic" the "go button", but you will need an alternative means. The user can get back to the calculate button by pressing BACK to close the soft keyboard. Or, you could move the calculate button elsewhere on the screen. Or, you could wrap the whole form in a ScrollView so the user can scroll down, even with the soft keyboard exposed, to get to the calculate button. Lot of possibilities here, just depends on your particular GUI design.Sirreverence
ok thats great, it gives me somewhere to start at least, I have considered moving the calc button though I'm working with the iphone images so they are a fixed layout in that sense. I'll see what I can come up with anyway. I think I'll get the rest of the functoinality working and come back to this. Thanks again for your helpRelativize
If not on ImeActions, then what should one rely on? I read that even keyevents may not be generated on some keypresses.Melody
C
11

I am detecting whether the DONE / GO / RETURN button has been pressed using an onEditorActionListener, but checking for IME options and KeyEvents to cover HTC keyboards as well as any keyboards that accept IME options.

(This code works for HTC Incredible keyboards as well any keyboard that has IME options)

EditText.setOnEditorActionListener(new TextView.OnEditorActionListener(){
    public boolean onEditorAction(TextView exampleView, int actionId, KeyEvent event){
        if(actionId == EditorInfo.IME_ACTION_DONE 
            || actionId == EditorInfo.IME_NULL
            || event.getKeyCode() == KeyEvent.KEYCODE_ENTER){

            //Do something in here
            return true;
        } else {
            return false;
        }
    }
});
Cadwell answered 24/8, 2011 at 17:57 Comment(4)
I fear, that with this code the //Do something here part will be executed twice .. could you check that?Decreasing
for me it gets ERROR/AndroidRuntime(26336): FATAL EXCEPTION: main java.lang.NullPointerException ------ on string "if(actionId ==..."Jurel
@Decreasing add a check for if(event.getAction() == KeyEvent.ACTION_UP) to prevent more then one callDolor
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.KeyEvent.getKeyCode()' on a null object referenceNide
S
2

I was using an EditText with inputType="number" and solved the problem by modifying Asha's solution:

private TextView.OnEditorActionListener numberEnterListener = new TextView.OnEditorActionListener(){
        public boolean onEditorAction(TextView tv, int actionId, KeyEvent event){
            if(actionId == EditorInfo.IME_ACTION_DONE 
                || actionId == EditorInfo.IME_NULL
                || event.getKeyCode() == KeyEvent.KEYCODE_ENTER){

                tv.clearFocus();

                //Stupid keyboard needs to be closed as well
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);

                return true;
            } else {
                return false;
            }
        }
    };

The focus was removed in order to stop showing the number pad. The imm was required because a soft keyboard was still present even after clearing focus.

Selfeffacement answered 1/2, 2014 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.