Android add placeholder text to EditText

How can I add a placeholder text to EditText in the class that isn't in the XML?

I have the following EditText in my code which will be shown in alertdialog:

    final EditText name = new EditText(this);

Ah, ok. What you're looking for is setHint(int) . Simply pass in a resource id of a string from your xml and you're good to go.

EDIT

And in XML, it's simply android:hint="someText"


android:hint="text" provides an info for user that what he need to fill in particular editText

for example :- i have two edittext one for numeric value and other for string value . we can set a hint for user so he can understand that what value he needs to give

android:hint="Please enter phone number"
android:hint="Enter name" 

after running app these two edittext will show the entered hint ,after click on edit text it goes and user can enter what he want (see luxurymode image)


In your Activity

<EditText
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:background="@null"
                android:hint="Text Example"
                android:padding="5dp"
                android:singleLine="true"
                android:id="@+id/name"
                android:textColor="@color/magenta"/>

在这里输入图像描述

链接地址: http://www.djcxy.com/p/70584.html

上一篇: 选择标记的占位符

下一篇: Android将占位符文本添加到EditText