Programmatically Set Edit Text Hint in Android?
Is There a Way to Set the Text Hint Displayed Inside of an Android Edittext with Java? I Would Like for My App to Be Able to Display Different Things in the...
is there a way to set the text hint displayed inside of an android edittext with java? I would like for my app to be able to display different things in the edittext at different times, thanks in advance
11 Answers
If you are using simple Editext without TextInputLayout :
EditText etUsername;
etUsername = (EditText) findViewById(R.id.etUsername);
etUsername.setHint("Your Hint");
If you are using Editext within TextInputLayout then you need to change property of TextInputLayout not Edittext:
TextInputLayout layoutUser;
layoutUser = (TextInputLayout) findViewById(R.id.inputLayoutUname);
layoutUser.setHint(getResources().getString(R.string.hintFaculty));
Call setHint() on the EditText.
((TextView) findViewById(R.id.yourEditTextId)).setHint("hint text");
Kotlin
editText.hint = "Your hint"
if you are using actionbarsherlock searchview, this is the best way to set string dynamically
AutoCompleteTextView searchTextView =
(AutoCompleteTextView) mSearchView.findViewById(R.id.abs__search_src_text);
searchTextView.setHint(getResources().getString(R.string.search));
for editText.hint you must delete name of your edit.Text from activity_main.xml then set in the MainActivity in java or activity_main.xml because text is preferred to hint.
<EditText
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine|textLongMessage"
android:hint="\n"/>
To be honest I don´t understand why.. but including "\n" as hint in the xml did the trick!
Now you can call setHint from Java code:
searchEditText.setHint(getString(R.string.search_hint));
and remove the \n from strings.xml if you want to allow the text split automatically according to the room
<string name="search_hint">keyword, location, max price (e.g. 1K, 1M)</string>
In Kotlin
val layoutMobile: TextInputLayout = findViewById<View>(R.id.inputLayout_MobileNumber) as TextInputLayout
layoutMobile.hint = "Your Hint"
in Java
yourEditTex.setText("");//First
yourEditTex.hit("Your text");
this for alls cases.
You can try following,
in Java
yourEditText.setHint("hint");
in Kotlin
yourEditText.hint = "Your hint"