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 edittext at different times, thanks in advance

1

11 Answers

Did you try the setHint?

myTextView.setHint("My conditional hint"); 

Hope it helps..

0

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.

0
((TextView) findViewById(R.id.yourEditTextId)).setHint("hint text"); 
0

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" 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like