Ruchir Baronia
Ruchir Baronia

Reputation: 7561

Simple edit text?

I am getting the users information from an edit text. I do have a listener that gets their entered information after clicking submit, but I want to also get the entered info after clicking back or clicking somewhere else:

enter image description here

For example, if the users clicks on the black space, I want to get the text they entered. If they type "hello", and click back rather than "enter", I still want to get the text hello. If, however, they don't type anything, I don't care about their input. How can I achieve this?

Thanks,

Ruchir

Upvotes: 1

Views: 85

Answers (2)

Inducesmile
Inducesmile

Reputation: 2493

First add these as a class variables

private String inputText;
private EditText yourEditText;

Get the instance of your EditText View

yourEditText = (EditText)findViewById(R.id.your_editText);

When a button is clicked, you can get the content of the EditText field like this

 Button mButton = (Button)findViewById(R.id.m_button);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            inputText = yourEditText.getText().toString();
        }
    });

If a user press the Back button, you can get the input if any like this

@Override
public void onBackPressed() {

    inputText = yourEditText.getText().toString();
    super.onBackPressed();
}

Then check if there is any value assigned to your String variable

if(inputText.equals("") || inputText == null){
      // there is no value            
}else{
      // there is value entered.  
}

To extend my solution for clicking some where else

add a class variable

private boolean isEditTextHasFocus;

then create a focus listener which will check if the Edittext has focus

 private View.OnFocusChangeListener focusListener = new View.OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus){
            isEditTextHasFocus = true;
        } else {
            isEditTextHasFocus  = false;
        }
    }
}

Add this line in onCreate(Bundle savedInstanceState) method

yourEditText.setOnFocusChangeListener(focusListener);

Then override to onTouchEvent(MotionEvent event) listener and access the Edittext input when the key up action is called

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        if(!isEditTextHasFocus){
            inputText = yourEditText.getText().toString();
        }
    }
    return true;
}

I hope this will give you a further idea to find your unique solution.

Upvotes: 1

Vrezh Gulyan
Vrezh Gulyan

Reputation: 228

Overriding what happens when the back button is pressed is bad practice and is unnecessary for what you want to do.

You need to use a special listener called onFocusChangedListener. This function is called anytime an element gains or loses focus. In this case for your editText it will be called whenever someone clicks on it or away. Pressing the back button or leaving the editText in any way will call this function. In the following code I check if

    if(!username.hasFocus())

which makes it so the value is only saved when focus from the editText is lost rather than everytime focus is changed.

You haven't added any of your own code so I am just going to use obvious placeholder variables in my code example.

Edittext username = (EditText findViewById(R.id.YOUR_EDITTEXTS_ID);
String previousValue = ""; // to keep track of value change
String usernameValue = "";
username.setOnFocusChangeListener(new View.OnFocusChangeListener(
{
   @Override
   public void onFocusChange(View v, boolean hasFocus) {

      if (username.hasFocus()){

        //take note of value for comparison when clicking away
        previousValue = username.getText().toString();

      } else if (!username.hasFocus()){

        // check if value has changed
       if (!previousValue.equals(username.getText().toString()){
           usernameValue = username.getText().toString();
       }

      }

}

});

Upvotes: 0

Related Questions