How to identify an input field by an input method service?

Viewed 89

I am implementing a custom keyboard and therefore use an input method service. In the onStartInput() method I am able to identify the app, the keyboard is used in, by the packageName of the EditorInfo parameter.

I would like to identy the input field to allow the user to execute certain actions depending on which field he has selected. For example if he uses app XY and select the first field their should be different keys offered to the user than when he selected the second or third field. All fields are regular text fields. To be able to do so, it is necessary to get an ID of the input field.

Does anyone now a solution for this?

1 Answers

From your question I think you want to identify the InputType of the keyboard to know which type of keyboard to show to a user. You can also do it in the onStartInputView like this:


    @Override
    public void onStartInputView(EditorInfo info, boolean restarting) {

        super.onStartInputView(info, restarting);
        int inputType = info.inputType & InputType.TYPE_MASK_CLASS;
                //Some input types have variation 
                //like passwords for InputType.TYPE_CLASS_TEXT
                //to handle that:
        int inputVariation = info.inputType & InputType.TYPE_MASK_VARIATION;

        switch (inputType){
            case InputType.TYPE_CLASS_TEXT:
                Log.i("InputType", "onStartInputView: Text");
                
                if(inputVariation == InputType.TYPE_TEXT_VARIATION_PASSWORD){
                    //letter input method for passwords
                }
                break;
            case InputType.TYPE_CLASS_NUMBER:
                Log.i("InputType", "onStartInputView: number");
                break;
            //Case ...:
            default:
                //Todo: what ever you prefer

        }
    }

For more information check out this link

Related