1

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();
}});
user1903022
  • 1,075
  • 1
  • 13
  • 19

2 Answers2

1

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;
            }
        });
bhavesh N
  • 787
  • 3
  • 9
  • 27
  • I am getting NPE. Could you please edit your solution in the existing code. My code for submit button is inside onCreate(). – user1903022 Aug 06 '15 at 05:59
0

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;
        }
    });
  • Even after implementing what you said, pressing enter is still taking me to the next line. Instead of taking me to the next line, I need the enter button to function just like the Submit button as shown in my code. – user1903022 Aug 06 '15 at 06:06