How can I set the text of an EditText?
This question is related to
android
textview
android-edittext
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
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);
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
editTextObject.setText(CharSequence)
http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)
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"/>
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
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));
You need to:
EditText in the xml file
EditText
in the activityEditText
This is the solution in Kotlin
val editText: EditText = findViewById(R.id.main_et_name)
editText.setText("This is a text.")
Source: Stackoverflow.com