make EditText lose focus on clicking a button

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

  • Android Studio “No tests were found”
  • Add ViewPagerIndicator to Android Studio
  • How to create toast from IntentService? It gets stuck on the screen
  • Try to use Window.FEATURE_CUSTOM_TITLE but got Exception:You cannot combine custom titles with other title feature..
  • Best way for convert speech to Pronunciation
  • Finding out Android Bluetooth LE GATT Profiles
  • Hide RecyclerView scrollbar
  • Android Studio Cannot Import Modules after upgrade
  • Mobile Web - Disable long-touch/taphold text selection
  • android dynamically change style at runtime
  • Unable to create Android Virtual Device
  • Show up-Button in actionBar in subscreen preferences
  • 7 Solutions collect form web for “make EditText lose focus on clicking a button”

    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

    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
        }
    
    }
    

    The best way I can find is

    private void listenToButton(){
        someButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                v.requestFocusFromTouch();
                //Proceed with things
            }
        }
    }
    

    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();
    
    }
    

    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();

    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(showUserListViewListener);
    

    Works for me 🙂

    Use following code on button click :-

    InputMethodManager imm = (InputMethodManager) view.getContext()
        .getSystemService(Context.INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.