Android add placeholder text to EditText
Asked Answered
P

8

434

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);
Punchy answered 22/11, 2011 at 2:1 Comment(2)
What do you mean by a placeholder?Absently
It's the text that is writen by light color on the text bars and when the user clicks on the textbar to write something that text will disappear.Punchy
A
953

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.

enter image description here

EDIT

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

Absently answered 22/11, 2011 at 3:7 Comment(2)
Thanks! I didn't know what they call it in android, I just know that on iOS it's called placeholder.Punchy
Its universally known as a placeholder, but of course android is different just to be different ;)Lucilelucilia
P
27

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)

Paternal answered 11/12, 2012 at 4:11 Comment(1)
This is called hint in android.Paternal
J
12

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"/>

enter image description here

Jennelljenner answered 30/8, 2015 at 14:23 Comment(0)
M
9

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.

Mazard answered 20/10, 2019 at 14:25 Comment(0)
B
7

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.

Blowpipe answered 11/10, 2013 at 6:52 Comment(0)
I
5

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

enter image description here

There you can add Hint(Place holder) to EditText

Ioves answered 26/7, 2017 at 7:8 Comment(0)
F
3

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);
Frink answered 22/11, 2011 at 2:19 Comment(0)
F
1

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"
Fettle answered 3/11, 2018 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.