Java – Is there a global way to capture changes in focus of all EditText in Android?

Is there a global way to capture changes in focus of all EditText in Android?… here is a solution to the problem.

Is there a global way to capture changes in focus of all EditText in Android?

Is there a clever way to avoid the first block code below being repeated a dozen times? The second block has the same form as the first, and I have several more blocks with the same form. I’m thinking about a set of EditText fields (good idea?). Bad [Why?] ]? But is there a global way to make a block that captures all changes in focus?

   txtExclude.setOnFocusChangeListener(new View.OnFocusChangeListener() {
       public void onFocusChange(View v, boolean hasFocus) 
       {
           if (!hasFocus)
               if (txtExclude.getText().length() == 0)
                   txtExclude.setBackgroundColor(Color.WHITE);

}});

txtRequired.setOnFocusChangeListener(new View.OnFocusChangeListener() {
       public void onFocusChange(View v, boolean hasFocus) 
       {
           if (!hasFocus)
               if(txtRequired.getText().length() == 0)
                   txtRequired.setBackgroundColor(Color.WHITE);
       }});

Edit

Non-working implementation of the first answer:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements View.OnFocusChangeListener
{
   @Override
   protected void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);  call superclass's version
      setContentView(R.layout.activity_main);  inflate the GUI

final EditText txtPattern = (EditText) findViewById(R.id.txtPattern);

final EditText txtLegal = (EditText) findViewById(R.id.txtLegal);

final EditText txtExclude = (EditText) findViewById(R.id.txtExclude);

final EditText txtRequired = (EditText) findViewById(R.id.txtRequired);

EditText txtLetters = (EditText) findViewById(R.id.txtLetters);

} // end method onCreate

@Override
    public void onFocusChange(View v, boolean hasFocus) {
        return;  breakpoint here **********************
    }
} // end class MainActivity

No matter which EditText gains or loses focus during debugging, it does not reach a breakpoint.

Solution

  1. You can use ViewTreeObserver and add onGlobalFocusChange listeners to your Root View layout. The following is sample code:

(Placing a line of 8 spaces here will format the first line of code.) )

    findViewById(R.id.yourRootContainer).getViewTreeObserver().addOnGlobalFocusChangeListener
    (
       new ViewTreeObserver.OnGlobalFocusChangeListener() 
       {
            @Override
            public void onGlobalFocusChanged(View oldFocus, View newFocus) 
            {
                whatever();
            }
       }
    );
  1. You can use custom focus listeners to differentiate using View IDs. The following is sample code:

    private class MyFocusChangeListener implements 
    View.OnFocusChangeListener 
    {
        @Override
        public void onFocusChange(View view, boolean hasFocus) 
        {
            if (view.getId() == R.id.my_view_id) 
            {
                doSomethingHere();
            }
        }
    }
    

And use it as:

myView1.setOnFocusChangeListener(new MyFocusChangeListener());

Related Problems and Solutions