0

I want to clear the focus of the edittext when the user clicks on the back button.

This is my onResume method :-

public void onResume() {
        super.onResume();

        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (i == keyEvent.KEYCODE_BACK) {
                    mEdtDob.clearFocus();
                    return true;
                }
                return false;

            }
        });

    }

I want to do that when i click on the back button the focus of the edittext should not be changed.

LoveAndroid
  • 186
  • 1
  • 11

3 Answers3

0

Solution: Code is correct. Check is parent view of EditText clickable? If it is not, after removing focus, EditText may gain focus again.

Add this line to parent view of EditText in xml:

android:focusable="true"
android:focusableInTouchMode="true"
Barış Söbe
  • 470
  • 4
  • 7
0
@Override public void onBackPressed() { 
int count =    getFragmentManager().getBackStackEntryCount(); 
if (count == 0) { 
super.onBackPressed();
mEdtDob.clearFocus();
//additional code 
} else       {     getFragmentManager().popBackStack(); 
} }

check this link:

How to implement onBackPressed() in Fragments?

Community
  • 1
  • 1
0

just add these two lines inside onKey Method.Code:

mEdtDob.setFocusable(true);
mEdtDob.setFocusableInTouchMode(true);

if this is nt working let me know in comments....

Ajay Chauhan
  • 1,471
  • 4
  • 17
  • 37