How to set text in EditText

How can I set the text of an EditText?

+103
android android-edittext textview
Jan 04 2018-11-11T00:
source share
10 answers

If you check the documents for EditText , you will find the setText() method. It accepts String and TextView.BufferType . For example:

 EditText editText = (EditText)findViewById(R.id.edit_text); editText.setText("Google is your friend.", TextView.BufferType.EDITABLE); 

It also inherits the TextView methods setText(CharSequence) and setText(int) , so you can set it the same way as a regular TextView :

 editText.setText("Hello world!"); editText.setText(R.string.hello_world); 
+210
Jan 04 2018-11-11T00:
source share
 String string="this is a text"; editText.setText(string) 

I 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

+19
Nov 06 '12 at 18:51
source share
 String text = "Example"; EditText edtText = (EditText) findViewById(R.id.edtText); edtText.setText(text); 

Check EditText to accept only String values, if necessary, convert them to string.

If int, double, long value, do:

 String.value(value); 
+4
Nov 13 '14 at 7:28
source share

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)); 
+3
Apr 15 '13 at
source share

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"/> 
+2
Jun 02
source share
+1
Jan 04 2018-11-11T00:
source share

You need:

  • Declare EditText in the xml file
  • Find EditText in action
  • Set the text to EditText
+1
Sep 24 '12 at 17:48
source share

This is a decision in Kotlin

 val editText: EditText = findViewById(R.id.main_et_name) editText.setText("This is a text.") 
+1
Aug 10 '18 at 19:30
source share

Solution in Android Java:

  1. Run your EditText, the identifier has come to your XML identifier.

     EditText myText = (EditText)findViewById(R.id.my_text_id); 
  2. in your OnCreate method, just set the text by a specific name.

     String text = "here put the text that you want" 
  3. use the setText method from your editText.

     myText.setText(text); //variable from point 2 
+1
Feb 13 '19 at 11:37
source share
 EditText editText = (EditText)findViewById(R.id.edit_text); editText.setText("Google is your friend.", TextView.BufferType.EDITABLE); 

Not to be too technical, and I'm sure you get it the same way I do, but it is “Editable”.

0
Mar 14 '14 at 4:34
source share



All Articles