Search code examples
androidtextviewandroid-edittext

How To Set Text In An EditText


How can I set the text of an EditText?


Solution

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