181

How can I set the text of an EditText?

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
user555910
  • 2,627
  • 6
  • 22
  • 28

11 Answers11

319

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);
redspidermkv
  • 503
  • 10
  • 25
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 7
    `EditText.BufferType.EDITABLE`? – sll Dec 19 '11 at 18:04
  • 3
    No, `EditText` extends `TextView`; `TextView.BufferType.EDITABLE` is the correct constant. – Kevin Coppock Dec 19 '11 at 21:23
  • 4
    What 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
  • 7
    Why 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
27

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")
Aman Babbar
  • 371
  • 3
  • 3
23
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

earlcasper
  • 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
6
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);
Imran Ali Khan
  • 8,469
  • 16
  • 52
  • 77
6

This is the solution in Kotlin

val editText: EditText = findViewById(R.id.main_et_name)
    editText.setText("This is a text.")
Aarón Lucker
  • 101
  • 1
  • 1
4

You can set android:text="your text";

<EditText
    android:id="@+id/editTextName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/intro_name"/>
Tom11
  • 2,419
  • 8
  • 30
  • 56
Cabezas
  • 9,329
  • 7
  • 67
  • 69
4

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

Dinesh
  • 1,410
  • 2
  • 16
  • 29
3

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));
Rob
  • 4,927
  • 12
  • 49
  • 54
Rajesh Wadhwa
  • 1,016
  • 2
  • 8
  • 14
1

You need to:

  1. Declare the EditText in the xml file
  2. Find the EditText in the activity
  3. Set the text in the EditText
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
1
editTextObject.setText(CharSequence)

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

Leif Andersen
  • 21,580
  • 20
  • 67
  • 100
1

Solution in Android Java:

  1. Start your EditText, the ID is come to your xml id.

    EditText myText = (EditText)findViewById(R.id.my_text_id);
    
  2. in your OnCreate Method, just set the text by the name defined.

    String text = "here put the text that you want"
    
  3. use setText method from your editText.

    myText.setText(text); //variable from point 2
    
Community
  • 1
  • 1