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

  • Custom watch face for Android Wear
  • How to make a custom Camera with live Stickers
  • How can I return back to my Activity from GPS settings window
  • Android circular progress bar with rounded corners
  • How do I get my app to appear in Google Now's Phone Search list?
  • cordova ubuntu: An error occurred while listing Android targets
  • Related posts:

    Fragment standard transition not animating
    How do I remove the selected tab indicator from the TabWidget?
    Failed to resolve: android.arch.lifecycle:extensions:1.0.0-alpha1 android studio 3.0
    Detect location spoofing in android?
    how to add WHERE clause to Query on android
    Is it possible to get suggested routes from google maps in android2.2 application?
  • Android Fatal Signal 11
  • RecyclerView ViewPager java.lang.NullPointerException android.support.v7.widget.RecyclerView$ItemAnimator$ItemHolderInfo.left
  • Download files and store them locally with Phonegap/jQuery Mobile Android and iOS Apps
  • Android clipPath equivalent for hardware accelerated View
  • Android freezes when there is a NullPointerException in OnClickListener.onClick (other tasks cannot start)
  • Android Web Service Tutorial
  • 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.