user1903022
user1903022

Reputation: 1085

Pressing Enter key should function exactly like some particular button

I am developing an Android App in which user needs to Register his account via his phone number. Now when the user have entered his number in the EditText View, he needs to press the Submit button to create his account, this submit button is present just below the EditText View.

As of now, after entering the number, when the user presses "Enter" key of his cellphone's keyboard, he is taken to next line of the EditText View, but, I want that when the user presses the Enter button, his account should be created. So, basically, I would want the Enter button to function exactly the way my Submit button is functioning.

Any help or guidance in this regard would be highly appreciated.

Code for my submit button:

final ImageButton bSubmit= (ImageButton) findViewById(R.id.bSubmit);
bSubmit.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
EditText eMobileNo = (EditText) findViewById(R.id.eMobileNo);
mobile = eMobileNo.getText().toString().replaceAll("[^\\d]", "");;
Log.i("MOBILE","MOBILE: "+mobile);
Toast.makeText(RegisterMe.this,"Your account has been successfully created",Toast.LENGTH_SHORT).show();

postData();
ConfirmToken();
finish();
}});

Upvotes: 1

Views: 227

Answers (2)

Syed Nazar Muhammad
Syed Nazar Muhammad

Reputation: 625

Try to get the Enter button Event & put your registration process in it:

 @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
        Register();
    }
}

Edit:

editText.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)) {
                Log.i(TAG,"Enter pressed");
            }    
            return false;
        }
    });

Upvotes: 0

bhavesh N
bhavesh N

Reputation: 787

You need to modified some code xml in Edittext tag

android:singleLine="true"
android:imeOptions="actionSend"

Then in your activity write the

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND) {
                    // submit code here


 EditText eMobileNo = (EditText) findViewById(R.id.eMobileNo);
mobile = eMobileNo.getText().toString().replaceAll("[^\\d]", "");;
Log.i("MOBILE","MOBILE: "+mobile);
Toast.makeText(RegisterMe.this,"Your account has been successfully created",Toast.LENGTH_SHORT).show();

postData();
ConfirmToken();
finish();

                    return  true;
                }

                return false;
            }
        });

Upvotes: 1

Related Questions