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)
This how to make input password that has hint which not converted to * !!.
On XML :
android:inputType="textPassword"
android:gravity="center"
android:ellipsize="start"
android:hint="Input Password !."
thanks to : mango and rjrjr for the insight :D.
In Android Studio you can add Hint (Place holder) through GUI. First select EditText field on designer view. Then Click on Component Tree Left side of IDE (Normally it's there, but it may be there minimized) There you can see Properties of selected EditText. Find Hint field as below Image
There you can add Hint(Place holder) to EditText
If you mean the location where you will add it in the layout. You can define a container like a FrameLayout and add this EditText to it when it is created.
<LinearLayout xmlns=".."/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
FrameLayout layout = (FrameLayout) findViewById(R.id.container);
layout.addView(name);
If you want to insert text inside your EditText view that stays there after the field is selected (unlike how hint behaves), do This:
In Java
// Cast Your EditText as a TextView
((TextView) findViewById(R.id.email)).setText("your Text")
In kotlin
// Cast your EditText into a TextView
// Like this
(findViewById(R.id.email) as TextView).text = "Your Text"
// Or simply like this
findViewById<TextView>(R.id.email).text = "Your Text"
来源:https://stackoverflow.com/questions/8221072/android-add-placeholder-text-to-edittext