Question

Layout:

    ....
    <EditText
        ....
        android:hint="@string/email"
        android:imeOptions="actionSend"/>
    <Button
        ...
        android:onClick="sendMessage"      <<<- both must call it 
        android:text="@string/send" />

Then binding in code:

( (EditText) findViewById(R.id.email) ).setOnEditorActionListener(new OnEditorActionListener()  {
            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                sendMessage(findViewById(android.R.id.content));
                return false;
            }
        });

Where sendMessage is

public void sendMessage(View view)
{
    ....
    intent.putExtra("email", getEditContent(R.id.email));   
    startActivityForResult(intent, 0);
}

When I press button everything is fine. When I press "Done" in imeOption (keyboard) two Activities starts at the same time.

What am I doing wrong?

Was it helpful?

Solution

Change the return value of the onEditorAction method from true to false.

Actually, I think the method is called twice because of the KeyEvent. Try logging the type of the arg2 parameter to check it. If you confirm this, instead of returning false, you can add an if/else to check the correct event.

OTHER TIPS

Chances are that your listener is receiving two different events. Try and debug onEditorAction method to check the values of KeyEvent arg2, to call your sendMessage method in the right event.

public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    if (event.getAction() != KeyEvent.ACTION_DOWN)
        return false;

    // do your stuff

    return true;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top