EditText editText = findViewById(R.id.edit_text); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editText.clearFocus(); } });
EditText editText = findViewById(R.id.edit_text); editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { editText.clearFocus(); } } });In this example, we set an OnFocusChangeListener on the EditText. When the focus changes (i.e., the user taps inside or outside the EditText), we check if the EditText has lost focus. If it has, we call the clearFocus() method to remove focus from it. Package library: android.widget