Android onfocuschange in xml

If you are using Android data binding to bind your handlers then you could write a custom BindingAdapter anywhere in your code to pass your handler from xml to java:

@BindingAdapter("app:onFocusChange")
public static void onFocusChange(EditText text, final View.OnFocusChangeListener listener) {
    text.setOnFocusChangeListener(listener);
}

And in Handler.java write a getter that returns an anonymous class:

public class Handler {
    public View.OnFocusChangeListener getOnFocusChangeListener() {
        return new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean isFocussed) {
                System.out.print("S");
            }
        };
    }
}

Now in your xml you can pass in the handler like this:

<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable name="handler" type="Handler" />
    </data>
    <EditText app:onFocusChange="@{handler.OnFocusChangeListener}"/>
</layout>

We can't set focus change listener of editext in xml file like onclick listener. We need to do it in Java file only.

edit_Text.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, 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();
    }
   }
});