How To Set Text In An EditText
AndroidTextviewAndroid EdittextAndroid 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
editTextObject.setText(CharSequence)
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)
Solution 10 - Android
You need to:
- Declare the
EditText in the xml file
- Find the
EditText
in the activity - Set the text in the
EditText
Solution 11 - Android
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