How to empty edittext without setText("");
Asked Answered
S

5

21

Is there a way to reset the edittext value without setting text like:

((EditText) findViewById(R.id.yoursXmlId)).setText("");

EDIT:

Textchanged listner is called when i use setText("").

Stratify answered 31/7, 2013 at 10:3 Comment(1)
if u want programmatically then u have to use this method i think.Otila
S
39

Another option is: EditText.getText().clear(); But you'll have to cast anyway:

((EditText) findViewById(R.id.yoursXmlId)).getText().clear();

Set answered 31/7, 2013 at 10:8 Comment(4)
good one but if he will use like this then he can also use setText("") also i thinkOtila
Thanks for the answer. Actually, I thought this answer would clear my bug. Still its the answer i was expecting :)Stratify
This works, but if you try to write something in the EditText again the initial text appears againFairchild
if anyone is getting the issue please use editText.post(() -> editText.getText().clear());Toaster
A
20

you can use following way

Kotlin:

myEditText.text.clear()

Java:

EditText myEditText = ((EditText) findViewById(R.id.yoursXmlId));
myEditText.getText().clear()
Aqua answered 31/7, 2013 at 10:17 Comment(0)
A
1

A function for that would be:

fun setTextViewEmpty(textView: TextView){
(textView as EditText ).text.clear()   }

inspired by this

Astraphobia answered 7/9, 2020 at 11:52 Comment(0)
N
0

I had a case where a Samsung device was not blanking out text with setText(""), so I used:

String BLANK = "\u0020"; // space character

setText (BLANK);

and it worked this way. It may have been a font issue.

Nicholasnichole answered 8/10, 2016 at 12:29 Comment(0)
T
0

if anyone is finding the selected answer not working please try with

editText.post(() -> editText.getText().clear());

or

editText.post(new Runnable() {
            @Override
            public void run() {
                editText.getText().clear();
            }
        });
Toaster answered 3/5, 2018 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.