How can I set the text of an EditText?
-
10i want to set text in edit text is it possible – user555910 Jan 04 '11 at 05:49
11 Answers
If you check the docs for EditText
, you'll find a setText()
method. It takes in a String
and a TextView.BufferType
. For example:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("This sets the text.", TextView.BufferType.EDITABLE);
It also inherits TextView
's setText(CharSequence)
and setText(int)
methods, so you can set it just like a regular TextView
:
editText.setText("Hello world!");
editText.setText(R.string.hello_world);

- 503
- 10
- 25

- 133,643
- 45
- 263
- 274
-
7
-
3No, `EditText` extends `TextView`; `TextView.BufferType.EDITABLE` is the correct constant. – Kevin Coppock Dec 19 '11 at 21:23
-
4What might confuse a newbie is that setText actually takes a CharSequence and a BufferType. So it's useful to remember that Strings are CharSequence's – Avatar33 Jun 11 '16 at 09:51
-
7Why does android.text.Editable exist or, better yet, why are normal developers supposed to navigate around it instead of EditText exposing an void setText(CharSequence) method? – Marcelo Lacerda Dec 14 '16 at 21:02
-
3@MarceloLacerda It does expose `setText(CharSequence)`, from its superclass `TextView`. So I'm not really sure why is this the most upvoted & accepted answer? – Hendy Irawan Jan 29 '18 at 05:23
-
It's been so long that I have asked this question that I have no idea what the original problem was. My best guess is that whatever I've found it awkward before got fixed in a new release of the API. – Marcelo Lacerda Jan 29 '18 at 15:49
-
It's true, a simple setText("String") works for me too. Not sure if there are any issues with that. – Tobias Reich May 23 '18 at 09:00
if you are using kotlin then it is gonna give you error if you are doing it like
editText.text = "some string"
just use it like
editText.setText("some string")

- 371
- 3
- 3
String string="this is a text";
editText.setText(string)
I have found String to be a useful Indirect Subclass of CharSequence
http://developer.android.com/reference/android/widget/TextView.html find setText(CharSequence text)
http://developer.android.com/reference/java/lang/CharSequence.html

- 915
- 10
- 8
-
Note all Strings are CharSequences, so this one works, but a raw CharSequence is not a String. If you have a raw CharSequence and REQUIRE a String, you need to call myCharSequence.toString() to get the official String. Not needed to know for THIS application, but sometimes elsewhere this is necessary. – DragonLord Jan 24 '20 at 00:45
String text = "Example";
EditText edtText = (EditText) findViewById(R.id.edtText);
edtText.setText(text);
Check it out EditText
accept only String values if necessary convert it to string.
If int, double, long value, do:
String.value(value);

- 8,469
- 16
- 52
- 77

- 61
- 1
- 1
This is the solution in Kotlin
val editText: EditText = findViewById(R.id.main_et_name)
editText.setText("This is a text.")

- 101
- 1
- 1
If you want to set text at design time in xml
file just simple android:text="username"
add this property.
<EditText
android:id="@+id/edtUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="username"/>
If you want to set text programmatically in Java
EditText edtUsername = findViewById(R.id.edtUsername);
edtUsername.setText("username");
and in kotlin
same like java using getter/setter
edtUsername.setText("username")
But if you want to use .text
from principle then
edtUsername.text = Editable.Factory.getInstance().newEditable("username")
because of EditText.text
requires an editable
at firstplace not String

- 1,410
- 2
- 16
- 29
Use +, the string concatenation operator:
ed = (EditText) findViewById (R.id.box);
int x = 10;
ed.setText(""+x);
or use
String.valueOf(int):
ed.setText(String.valueOf(x));
or use
Integer.toString(int):
ed.setText(Integer.toString(x));

- 4,927
- 12
- 49
- 54

- 1,016
- 2
- 8
- 14
You need to:
- Declare the
EditText in the xml file
- Find the
EditText
in the activity - Set the text in the
EditText

- 14,081
- 3
- 67
- 82

- 11
- 1
editTextObject.setText(CharSequence)
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)

- 21,580
- 20
- 67
- 100
Solution in Android Java:
Start your EditText, the ID is come to your xml id.
EditText myText = (EditText)findViewById(R.id.my_text_id);
in your OnCreate Method, just set the text by the name defined.
String text = "here put the text that you want"
use setText method from your editText.
myText.setText(text); //variable from point 2

- 1
- 1

- 11
- 1