I am setting drawable right for EditText like below,
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_clear_black_24dp), null);
I am setting drawable left for EditText in xml. I want to set it's visibility to visible or hide.How Do I do that programmatically.
I have EditText for Search. While start typing I am setting clear icon programatically.
Clear icon will clear text in EditText. When clear icon without text is clicked I want to close keyboard and make clear icon hide. Below is my code for that,
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setCursorVisible(true);
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(getApplicationContext(), R.drawable.ic_clear_black_24dp), null);
}
});
editText.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
if(editText.getCompoundDrawables()[2]!=null){
if(event.getX() >= (editText.getRight()- editText.getLeft() - editText.getCompoundDrawables()[2].getBounds().width())) {
if(!editText.getText().toString().equals("")) {
editText.setText("");
}
else {
// getWindow().setSoftInputMode(
// WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
editText.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
closeKeyboard();
editText.setCursorVisible(false);
}
}
}
}
return false;
}
});
I want to hide it programmatically.
Just pass null in setCompoundDrawablesWithIntrinsicBounds() method to hide Drawable from your editText
SAMPLE CODE
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With