Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a clever way to avoid repeating the first block of code below a dozen times? The second block is identical in form to the first and I have several more that will have the same form. I'm thinking about an array of EditText fields (good idea? bad [why?]?) but is there a global way to make one block catch 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 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 gained or lost focus during debug, breakpoint was not reached.

like image 322
DSlomer64 Avatar asked Aug 29 '15 17:08

DSlomer64


2 Answers

  1. You can use ViewTreeObserver and add the onGlobalFocusChange listener on your root view layout. Below is the sample code :

(Putting a line with 8 blank spaces here will format first line(s) 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 listener, differentiate using views id. Below 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());
like image 176
Sumit T Avatar answered Nov 14 '22 23:11

Sumit T


Here is an other way.

Make a CustomEditext class in your package by subclassing the EditText like below

Let your package is com.android.app

public class CustomEditText extends EditText {

public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onFocusChanged(boolean focused, int direction,
        Rect previouslyFocusedRect) {

    if (!focused)
        if (getText().length() == 0)
            setBackgroundColor(Color.WHITE);

    super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
}

you can use this customEditText in your layout like below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<com.android.app.CustomEditText
    android:id="@+id/com.example.customedittext1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Name"
    android:inputType="textPersonName" >
</com.android.app.CustomEditText>

<com.android.app.CustomEditText
    android:id="@+id/edEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Email"
    android:inputType="textPersonName" >
</com.android.app.CustomEditText>

<com.android.app.CustomEditText
    android:id="@+id/edContact"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Contact"
    android:inputType="textPersonName" >
</com.android.app.CustomEditText>

You will not need to set onFocusChangedListner() on every Edit Field.

Hope this helps

like image 34
Qadir Hussain Avatar answered Nov 15 '22 01:11

Qadir Hussain