How To Set Text In An EditText

AndroidTextviewAndroid Edittext

Android Problem Overview


How can I set the text of an EditText?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

This is the solution in Kotlin

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

Solution 6 - Android

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

Solution 7 - Android

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

Solution 8 - Android

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

Solution 9 - Android

Solution 10 - Android

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

Solution 11 - Android

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
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser555910View Question on Stackoverflow
Solution 1 - AndroidKevin CoppockView Answer on Stackoverflow
Solution 2 - AndroidearlcasperView Answer on Stackoverflow
Solution 3 - AndroidAman BabbarView Answer on Stackoverflow
Solution 4 - AndroidSamrat Das ChoudhuryView Answer on Stackoverflow
Solution 5 - AndroidAarón LuckerView Answer on Stackoverflow
Solution 6 - AndroidCabezasView Answer on Stackoverflow
Solution 7 - AndroidRajesh WadhwaView Answer on Stackoverflow
Solution 8 - AndroidDineshView Answer on Stackoverflow
Solution 9 - AndroidLeif AndersenView Answer on Stackoverflow
Solution 10 - Androiduser1695144View Answer on Stackoverflow
Solution 11 - AndroidVinicius MesquitaView Answer on Stackoverflow