Reputation: 6175
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);
Upvotes: 437
Views: 424958
Reputation: 139
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"
Upvotes: 1
Reputation: 101
You have to use the android:hint attribute
<EditText
android:id="@+id/message"
android:hint="<<Your placeholder>>"
/>
In Android Studio, you can switch from XML -> Design View and click on the Component in the layout, the EditText field in this case. This will show all the applicable attributes for that GUI component. This will be handy when you don't know about all the attributes that are there.
You would be surprised to see that EditText has more than 140 attributes for customization.
Upvotes: 10
Reputation: 5514
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
Upvotes: 5
Reputation: 3712
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"/>
Upvotes: 12
Reputation: 1384
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.
Upvotes: 7
Reputation: 33771
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.
And in XML, it's simply android:hint="someText"
Upvotes: 957
Reputation: 3215
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)
Upvotes: 27
Reputation: 11230
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);
Upvotes: 3