Change the behaviour of "imeOptions" (Android)

1.1k views Asked by At

This is my problem right now, guys.

I have the next TextView and EditText

activity_profile.xml

<TextView
            android:layout_marginEnd="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/done"
            android:id="@+id/done_profile_btn"
            android:textColor="@color/white"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />
<EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:id="@+id/input_profile_swift"
            android:hint="@string/swift"
            android:inputType="textCapCharacters"
            android:textColor="@color/colorPrimaryDark"
            android:layout_marginBottom="22dp"
            android:maxLines="1"
            android:imeOptions="actionDone"/>

And I would like to change the action that press the 'Done' button on the keyboard does for the EditText in order to do the same as the TextView, which does the next:

ProfileActivity.java

done_btn = (TextView) findViewById(R.id.done_profile_btn);
swift = (EditText)findViewById(R.id.input_profile_swift);
done_btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //saveChanges();
                //finish();
                Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
            }
        });
...

So... I want that both (press 'Done' on my keyboard and press the TextView) do the same.

Any idea of how to do that? Thank you very much.

4

There are 4 answers

0
Rahul On BEST ANSWER

You can use this one also (sets a special listener to be called when an action is performed on the EditText), it works both for DONE and RETURN:

swift.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
              //saveChanges();
              //finish();
              Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
            }    
            return false;
        }
    });

Hope it will help !

0
kamlesh gorasiya On

try this

EditText swift = (EditText)findViewById(R.id.input_profile_swift);
swift.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            Toast.makeText(getApplicationContext(), "Your changes have been saved", Toast.LENGTH_SHORT).show();
        }

    }
});

You have to implements OnEditorActionListener interface and set it to your EditText. using setOnEditorActionListener(OnEditorActionListener implimentation.)

0
Zhenya  Rechun On

Use TextView.OnEditorActionListener.

@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
    KeyboardHelper.hideSoftKeyboard(getActivity());
    doSomeWoorrkThatYouNeed();
    return true;
}
0
Manos On

You should add this android:clickable="true" to your xml for the TextView. Also you could set an EditorActionListener using setOnEditorActionListener on your EditText in similar way that you did with setOnClickListener for your TextView.