Ayush Baheti
Ayush Baheti

Reputation: 183

How to handle android keyboard actions

I am new to Android. I am trying to make a text box and on pressing done key, it should take the value to the java code. For this I am using setOnEditorActionListener.. I searched on how to do this and got many answers on how to implement it. Example:

EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            sendMessage();
            handled = true;
        }
        return handled;
    }
});

I need to ask where should I write this thing? In which method? I tried doing it in onCreate but it threw some error. I somehow made it work using this code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.unlock);
        Log.i(TAG, "onCreate");
        editText= (EditText) findViewById(R.id.editText);
        editText.setOnEditorActionListener(this);
    }


    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

        boolean handled = false;
        if (actionId == EditorInfo.IME_ACTION_SEND) {
            Log.i(TAG, "button pressed");
            Toast.makeText(this, "Hey you just clicked the DONE button", Toast.LENGTH_SHORT).show();
            handled = true;
        }
        return handled;  
      }

Here I used this keyword, and I don't understand why have I used it. Question 1. Please help me understand, why have we used this keyword..

Question 2. Why wasn't it working in the below code?

 public void checkInput() {
        Log.i(TAG, "Enter checkInput method");

        final EditText editText= (EditText) findViewById(R.id.editText);

        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                Log.i(TAG, "Enter onEditorAction");
                boolean handled = false;
                if (actionId == EditorInfo.IME_ACTION_SEND) {
                    Log.i(TAG, "button pressed")
                    handled = true;
                }
                return handled;
            }
        });
    }

I called this checkInput method from onCreate.

Upvotes: 1

Views: 618

Answers (1)

Oladapo Omonayajo
Oladapo Omonayajo

Reputation: 980

To answer Question 1:

Here I used this keyword, and I don't understand why have I used it. Question 1. Please help me understand, why have we used this keyword..

You're telling Java to look into the Activity class for implementations of methods required by the TextView.OnEditorActionListener interface. So for all interactions with your soft keyboard, Java would look into your class for the method: onEditorAction

In order for the above to work, your activity needs to defined like:

public class MyActivity implements TextView.OnEditorActionListener {}

For question 2:

Question 2. Why wasn't it working in the below code?

To check for the "Done" action, your if statement should be:

if (actionId == EditorInfo.IME_ACTION_DONE) { ... }

Hope that helps.

Upvotes: 1

Related Questions