25

I have two EditText fields in my activity and a done button. I want both the EditText fields to loose focus (that is the cursor should not not be displayed on either of them) when the user presses the button. I am using the following code:

final Button saveButton = (Button) findViewById(R.id.saveButton);
    saveButton.setOnClickListener(saveButtonListener);

private OnClickListener saveButtonListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
                Text1.clearFocus();
                Text2.clearFocus();

    }

}

However, when I press the done button, the cursor comes up on Text1 even if I haven't clicked on any EditText yet. How can I make the EditText fields loose focus on the click of the button

Ankush
  • 6,767
  • 8
  • 30
  • 42

8 Answers8

33

try by Changing your code as:

private OnClickListener saveButtonListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
                Text1.clearFocus();
                Text2.clearFocus();
                saveButton.requestFocus(); //or any other View
    }

}

because as doc as about public void clearFocus () :

Called when this view wants to give up focus. If focus is cleared onFocusChanged(boolean, int, android.graphics.Rect) is called.

Note: When a View clears focus the framework is trying to give focus to the first focusable View from the top. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after wich the framework will give focus to this view.

means you must set Focus to other view on button click because Text1 act as first View in your layout

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
5

you can try this code just move focus to another view.

private OnClickListener saveButtonListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        saveButton.requestFocus(); //any other View
    }

}
Juan Andrés Diana
  • 2,215
  • 3
  • 25
  • 36
neelesh
  • 51
  • 2
4

The best way I can find is

private void listenToButton(){
    someButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            v.requestFocusFromTouch();
            //Proceed with things
        }
    }
}
  • 1) Create a static reference to an edit text. 2) In the setOnFocusChange method set the static edit text equal to the edit text whose focus was gained 3) In the button click method use this reference to know who last contained focus, even if their focus is not lost due to the button click – Andre L Torres Oct 01 '15 at 01:20
3

This works for me:

@Override
public void onClick(View v) {

     //To hide soft keyboard          
   InputMethodManager im = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
   im.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

   v.requestFocusFromTouch();

}
2

Set these lines for that button will do the trick.

View.OnClickListener listener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        v.setFocusable(true);
        v.setFocusableInTouchMode(true);
        v.requestFocus();
        ......
    }
};
button.setOnClickListener(listener);

Works for me :)

Chandler
  • 3,061
  • 3
  • 22
  • 30
0

You can just create an empty hidden EditText element in your layout. Something with 0 width and 0 height. Then in your click you can call hiddenEditText.requestFocus();

Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53
0

Chandler's answer worked for me but I had to tap twice on the button (bcoz of the focusable property):

1st tap: Gained focus on the button.

2nd tap: Clicked the button.

So, with his help, you can achieve your goal in the following way as well (with single tap on the button):

Take any view from your layout (it can be a parent-layout or some other text-view) and put the focusable property in that view, like:

<LinearLayout // In my case its a parent view with LinearLayout, but this can be any other type of view.
    android:id="@+id/linearLayoutParent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"    <--- Required
>

Then, in your OnClickListener:

final Button saveButton = (Button) findViewById(R.id.saveButton);
saveButton.setOnClickListener(saveButtonListener);

private OnClickListener saveButtonListener = new OnClickListener() {
    @Override
    public void onClick(View v) {
            Text1.clearFocus();
            Text2.clearFocus();

            findViewById(R.id.linearLayoutParent).requestFocus(); // puts the focus on that focusable view.
    }
}

Bonus:

After losing focus if you need to hide soft keyboard as well, check this and this.

  • @ASMSayem No, I only tried to provide a helping answer to this SO question, without asking anything for myself. Am I not clear somewhere in my answer above? – AbdulMoizHussain May 04 '22 at 19:34
-1

Use following code on button click :-

InputMethodManager imm = (InputMethodManager) view.getContext()
    .getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
Shyam
  • 6,376
  • 1
  • 24
  • 38