1. Are you ready for the Galaxy S20? Here is everything we know so far!

Dynamically Change Hint Text In EditText Field?

Discussion in 'Android Development' started by Laxidasical, Sep 8, 2011.

  1. Laxidasical

    Laxidasical Newbie
    Thread Starter

    Is there a way to change the hint text used in EditText fields??? I've figured out how to alter the value, but not the hint text. I thought there would be something like this code below, but I can't find a function that does this:

    Code (Text):
    1. findViewById(R.id.myEditTextField).[B][COLOR="Sienna"]setHint([/COLOR][/B]"New Hint Text"[B][COLOR="Sienna"])[/COLOR][/B];
    Right now I'm using a workaround. For each text field that requires an alternate hint text, I created a second field with the alternate hint in place and then switch the visibility between the two depending on certain settings. While this works for the most part it's definitely not an elegant solution!


    I figured it out. The EditText id must be set to set to a variable and casted as EditText, then setHint() can be used on that variable. For example...

    Code (Text):
    1. EditText text = (EditText)findViewById(R.id.myEditTextField);
    2. text.setHint(R.string.myHintString);
    Note I: Make sure to import android.widget.EditText.
    Note II: I'm passing an id for a entry in strings.xml, but you can enter text directly.

    alostpacket likes this.

    1. Download the Forums for Android™ app!


  2. alostpacket

    alostpacket Over Macho Grande?

    thanks for posting back the solution :D
  3. Laxidasical

    Laxidasical Newbie
    Thread Starter

    No problem!

Share This Page