user555910
user555910

Reputation: 2667

How To Set Text In An EditText

How can I set the text of an EditText?

Upvotes: 185

Views: 378888

Answers (11)

Aman Babbar
Aman Babbar

Reputation: 391

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")

Upvotes: 29

Kevin Coppock
Kevin Coppock

Reputation: 134714

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);

Upvotes: 322

Dinesh
Dinesh

Reputation: 1670

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

Upvotes: 5

Vinicius Mesquita
Vinicius Mesquita

Reputation: 11

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
    

Upvotes: 1

Aar&#243;n Lucker
Aar&#243;n Lucker

Reputation: 111

This is the solution in Kotlin

val editText: EditText = findViewById(R.id.main_et_name)
    editText.setText("This is a text.")

Upvotes: 6

Samrat Das Choudhury
Samrat Das Choudhury

Reputation: 61

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);

Upvotes: 6

Cabezas
Cabezas

Reputation: 10767

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"/>

Upvotes: 4

Rajesh Wadhwa
Rajesh Wadhwa

Reputation: 1036

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));

Upvotes: 3

earlcasper
earlcasper

Reputation: 915

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

Upvotes: 23

user1695144
user1695144

Reputation: 11

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

Upvotes: 1

Related Questions