172

Can anyone suggest to me any event related to the focus of the EditText? My application contains an EditText, which accepts a URL in it.

Now my problem is that, that after the user will enter the URL in the field and Move further, without any of the click events, i.e when the focus will move from the EditText, it should detect the entered Url and goes to the server.

If I get the reply using Json Parsing, then it'll be more convenient.

Rahul
  • 3,293
  • 2
  • 31
  • 43
Sheetal Shelar
  • 1,731
  • 2
  • 11
  • 5

4 Answers4

554

Here is the focus listener example.

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if (hasFocus) {
            Toast.makeText(getApplicationContext(), "Got the focus", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(getApplicationContext(), "Lost the focus", Toast.LENGTH_LONG).show();
        }
    }
});
GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57
Pratik
  • 30,639
  • 18
  • 84
  • 159
  • In fragments inside focus change listener i am unable to update my UI, any idea? – moDev Jul 03 '13 at 20:57
  • 4
    This does not work. If the keyboard was hide by "enter" or hide keyboard button on some devices, the "lost the focus" won't be fired. If you focus on edit text again, it won't file "got the focus" either. – SnowWolf May 26 '16 at 19:28
  • 1
    Be aware that if you have your layout set to resize when the soft keyboard is displayed, or do something ui-related using a TextWatcher, the listener may get called a lot - even twice for each character typed! https://stackoverflow.com/questions/14727248/edittext-loses-focus-when-keyboard-appears-requires-touching-twice-to-edit – M_M Sep 19 '18 at 13:55
35

When in Kotlin it will look like this:

editText.setOnFocusChangeListener { _, hasFocus ->
    if (hasFocus) {
        toast("focused")
    } else {
        toast("focuse lose")
    }
}
Boken
  • 4,825
  • 10
  • 32
  • 42
arjava
  • 1,719
  • 2
  • 17
  • 21
20
  1. Declare object of EditText on top of class:

    EditText myEditText;
    
  2. Find EditText in onCreate Function and setOnFocusChangeListener of EditText:

    myEditText = findViewById(R.id.yourEditTextNameInxml); 
    
    myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                 @Override
                 public void onFocusChange(View view, boolean hasFocus) {
                     if (!hasFocus) {
                          Toast.makeText(this, "Focus Lose", Toast.LENGTH_SHORT).show();
                     }else{
                         Toast.makeText(this, "Get Focus", Toast.LENGTH_SHORT).show();
                     }
    
                 }
             });
    

It works fine.

Boken
  • 4,825
  • 10
  • 32
  • 42
Wajid khan
  • 842
  • 9
  • 18
4

For those of us who this above valid solution didnt work, there's another workaround here

 searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View view, boolean isFocused) {
            if(!isFocused)
            {
                Toast.makeText(MainActivity.this,"not focused",Toast.LENGTH_SHORT).show();

            }
        }
    });
Dev_Man
  • 847
  • 1
  • 10
  • 28