I need to show a clear button at the right of an edit text and hide it when the text is"". How can I do that.
I just need to know how to show and hide it when the text length>0 or =0, nothing more.
I need to show a clear button at the right of an edit text and hide it when the text is"". How can I do that.
I just need to know how to show and hide it when the text length>0 or =0, nothing more.
Here is your answer.
Use code and make button visibility on and off on textchange by check character length.
Take a look at below code, it may help you :
EditText etSearch = (EditText) view.findViewById(R.id.etSearch);
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String str = etSearch.getText().toString();
if (str.isEmpty()) {
btnClear.setVisibility(View.INVISIBLE);
} else {
btnClear.setVisibility(View.VISIBLE);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
Text-watcher may work for you
If(editText1.getText.toString.length>0)
{
if(button1.getVisibility == View.GONE)
{
button1.setVisibility(View.VISIBLE)
}
}
else
{
if(button1.getVisibility == View.VISIBLE)
{
button1.setVisibility(View.GONE)
}
}
Set It's Visibility GONE from xml.
If(editText1.getText.toString.length>0)
{
if(button1.getVisibility == View.GONE)
{
button1.setVisibility(View.VISIBLE)
}
}
else
{
if(button1.getVisibility == View.VISIBLE)
{
button1.setVisibility(View.GONE)
}
}
Try this code
Field1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() != 0)
// set the visibility
}
});
You can use a TextWatcher
, a type of Listener:
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
//Here, you can check for text size...
int length = editText.toString().length();
[...]
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}
Taken from this question.