Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put default value in EditText in Android Studio

I am trying to put a value in EditText but I can not.

The code I edit:

<EditText android:textColor="@color/white" android:id="@id/input_username" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:layout_marginTop="20.0dip" android:hint="@string/input_username_hint" 
    android:ems="10" android:singleLine="true" android:maxLength="140" 
    android:layout_below="@id/input_room_name" android:layout_centerHorizontal="true" />

When I put android:text="user" the genarated apk is not opening.

like image 452
Naveen Kumar Avatar asked Aug 16 '15 14:08

Naveen Kumar


People also ask

How to set default value in EditText in android Studio?

You can use EditText. setText(...) to set the current text of an EditText field. Save this answer.

How do I change my EditText value?

Set the Text of Android EditText In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file. Following is the example to set the text of TextView control while declaring it in XML Layout file.

What is inputType in android Studio?

The android:inputType attribute allows you to specify various behaviors for the input method. Most importantly, if your text field is intended for basic text input (such as for a text message), you should enable auto spelling correction with the "textAutoCorrect" value.


2 Answers

Do this:

editText.setText("Your default text");

But if you want to show that gray text that disappears when the user starts to type, you're looking for android:hint.

On xml:

android:hint="Your default text"

On java:

editText.setHint("Your default text");

On kotlin:

editText.hint = "Your default text"
like image 118
Mauker Avatar answered Sep 24 '22 15:09

Mauker


In your .xml file,

specify

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="user" />

or

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="user" />

If you want to do it in program

do this

EditText et = (EditText)findViewById(R.id.your_edittext_id);
et.setText("user");

or

et.setHint("user");
like image 29
pptang Avatar answered Sep 23 '22 15:09

pptang