2

Is there any way I can force show Android's SoftKeyboard in NumberPassword mode without having an actual EditText in my activity?

I managed to show the keyboard when the activity starts by adding android:windowSoftInputMode="stateAlwaysVisible" on my AndroidManifest.xml, make it impossible to close by overriding onKeyPreIme in my CustomView class that extends TextView, and handling the touch events by myself by overriding onKeyUp in my Activity.

If I add android:inputType="numberPassword" directly in CustomView's XML Layout, Activity's onKeyUp gets bypassed and the keyboard write characters in my CustomView and KEYCODE_ENTER closes my keyboard.

What I want to achieve is:

  • SoftKeyboard always out, both on Activity Start and Resume from the background
  • Cannot be closed with KEYCODE_ENTER or KEYCODE_BACK
  • 9-digit layout + Backspace
  • Handle by myself key pressure to make it do something else instead of writing characters
Cusy
  • 233
  • 1
  • 3
  • 11
  • Can you show some code to have a better picture of what you are doing? – BitParser May 20 '16 at 08:46
  • Something very close to this https://stackoverflow.com/questions/13591012/implementing-onkeypreimeint-keycode-keyevent-event-in-fragment/16108133#16108133 but without EditText. Numbers / Backspace will fire different methods based on the key pressed. That solution isn't working because, at least on my 6.0.1 Moto X, the KEYCODE_ENTER cause the keyboard to disappear. – Cusy May 20 '16 at 08:54

1 Answers1

2

Taken from https://developer.android.com/training/keyboard-input/commands.html for convenience:

Both the Activity and View class implement the KeyEvent.Callback interface, so you should generally override the callback methods in your extension of these classes as appropriate.

I suggest you to override the default implementation of onKeyUp in your CustomView class, and make the CustomView.onKeyUp method redirect the event to your Activity's onKeyUp method.

As an example:

public class CustomView extends TextView {
    private KeyEvent.Callback myKeyEventCallback;

    public void setCustomKeyEventCallback(KeyEvent.Callback callback) {
        myKeyEventCallback = callback;
    }

    ...

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        return myKeyEventCallback.onKeyUp(keyCode, event);
    }
}

And in your Activity do this:

CustomView view = ...; // here you take the reference to your custom view
view.setCustomKeyEventCallback(new KeyEvent.Callback() {
    // ... other methods

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // this calls your activity's implementation of onKeyUp
        MyActivity.this.onKeyUp(keyCode, event);
        return false; // prevent event from firing twice
    }
});

This will help you redirect the onKeyUp method calls from your CustomView to your Activity's onKeyUp implementation.

BitParser
  • 3,748
  • 26
  • 42
  • With you solution I get the events fired twice, once by CustomView's onKeyUp and once by Activity's onKeyUp. I just set the callback's onKeyUp method to always return false, so I got event fired only once. – Cusy May 20 '16 at 09:44