Reputation: 2667
How can I set the text of an EditText?
Upvotes: 185
Views: 378888
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
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
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
Reputation: 11
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
Upvotes: 1
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
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
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
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
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
Reputation: 11
You need to:
EditText in the xml file
EditText
in the activityEditText
Upvotes: 1
Reputation: 22342
editTextObject.setText(CharSequence)
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)
Upvotes: 1