Question

please find the updated code below.

Following is the code:

package com.Wase.edittext;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.Toast;
import com.Wase.edittext.R;
import android.widget.TextView;

public class MyAndroidAppActivity extends Activity {

 private EditText edittext;
 private EditText edittext1;

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_android_app);

    addKeyListener();
 edittext.requestFocus();
}



 public void addKeyListener() {

    // get edittext component
    edittext = (EditText) findViewById(R.id.editText);
    edittext1 = (EditText) findViewById(R.id.editText1);



    // add a keylistener to keep track user input
    edittext.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {

    // if keydown and "enter" is pressed
    if ((event.getAction() == KeyEvent.ACTION_DOWN)
        && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
    {
        edittext1.requestFocus();
    }

    edittext1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
         @Override
         public boolean onEditorAction(TextView a, int b, KeyEvent c) {

    // if keydown and "enter" is pressed
    if ((c.getAction() == KeyEvent.ACTION_DOWN)
        && (c.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {

        //hide the keyboard
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
                imm.hideSoftInputFromWindow(edittext1.getWindowToken(), 0);
    // display a floating message
        Toast.makeText(MyAndroidAppActivity.this,
            edittext.getText() + " " + edittext1.getText(), Toast.LENGTH_SHORT).show();
        return true;
    }

        return false;
}
 });
        return false;
}
    });
 }
}

This code is only for keyboard click or system keyboard click. I want to hide the virtual key board and display the toast message on click on enter key of android virtual key board.

Please help me on this.

Was it helpful?

Solution

You are setting a new onKeyListener to edittext1 everytime a key is pressed in edittext... is that actually what you want to do?

The code you've got to detect when the enter key is pressed seems fine. You will need to add the following code to close the keyboard and do a toast.

editText.setOnEditorActionListener(new TextView.OnEditorActionListener(){
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER))
            {
                InputMethodManager imm = (InputMethodManager)getSystemService(
                    Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
                Toast.makeText(MyAndroidAppActivity.this, "My message", Toast.LENGTH_SHORT).show();
            }
       }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top