Android:hint property doesn't work with EditText

I have an strange behavior with one screen.

I have an EditText and I set the hint property (in code and in the axml). However, the screen does not render it.

In code
myUserEmail = FindViewById(Resource.Id.emailUser);
myUserEmail.SetHint(Resource.String.loginHintPassword);

In Layout

Anyone can give a possible to solve this?

Tagged:

Posts

  • CortezCortez USMember ✭✭✭

    Hello @juanacosta !

    Are you sure that this works?

    I think the cast missing next to the FindViewById method. Without that the compiler show me an error.

    I tried this and worked.

    [Activity (Label = "Hint", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { EditText myUserEmail; protected override void OnCreate (Bundle bundle) { base.OnCreate (bundle); // Set our view from the "main" layout resource SetContentView (Resource.Layout.Main); myUserEmail = FindViewById<EditText> (Resource.Id.emailUser); myUserEmail.SetHint (Resource.String.loginHintPassword); } }

  • juanacostajuanacosta AUMember

    My complete code is this

    [Activity(Label = "@string/app_name")]
    public class Program : Activity
    {
    #region properties
    private Button buttonRegister;
    private Button buttonLogin;
    private EditText myUserEmail;
    private EditText myUserPassword;
    private EditText myUserReTypePassword;
    #endregion

        #region activity lifecycle
    
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.login);
            loadButtons();
        }
    
        #endregion 
    
        #region helpers
    
        private void loadButtons()
        {
            myUserEmail = FindViewById<EditText>(Resource.Id.emailUser);
            myUserEmail.SetHint(Resource.String.loginHintPassword);
            myUserEmail.SetOnKeyListener(new LoginTextAction(this));
            myUserPassword = FindViewById<EditText>(Resource.Id.passwordUser);
            myUserPassword.SetHint(Resource.String.loginHintPassword);
            myUserPassword.SetOnKeyListener(new LoginTextAction(this));
            myUserReTypePassword = FindViewById<EditText>(Resource.Id.passwordUserReType);
            myUserReTypePassword.SetOnKeyListener(new LoginTextAction(this));
            buttonRegister = FindViewById<Button>(Resource.Id.btnRegister);
            buttonRegister.SetOnClickListener(new LoginAction(this));
            buttonLogin = FindViewById<Button>(Resource.Id.btnLogin);
            buttonLogin.SetOnClickListener(new LoginAction(this));
        }
    
        #endregion 
    
    }
    

    And it doesn't work.

    Any other suggestions?

  • CortezCortez USMember ✭✭✭
    edited April 2015

    For me your application perfectly working. Try to save all file, clean the project.

    login.axml
    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:id="@+id/emailUser" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/passwordUser" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="hinta" /> <EditText android:id="@+id/passwordUserReType" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnRegister" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Register" /> <Button android:id="@+id/btnLogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login" /> </LinearLayout>

    Strings.xml
    <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Click Me!</string> <string name="app_name">Hint</string> <string name="loginHintPassword">puffin</string> </resources>

    MainActivity.cs
    [Activity(Label = "@string/app_name", MainLauncher = true)] public class Program : Activity { #region properties private Button buttonRegister; private Button buttonLogin; private EditText myUserEmail; private EditText myUserPassword; private EditText myUserReTypePassword; #endregion #region activity lifecycle protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.login); loadButtons(); } #endregion #region helpers private void loadButtons() { myUserEmail = FindViewById<EditText>(Resource.Id.emailUser); myUserEmail.SetHint(Resource.String.loginHintPassword); myUserEmail.SetOnKeyListener(new LoginTextAction(this)); myUserPassword = FindViewById<EditText>(Resource.Id.passwordUser); myUserPassword.SetHint(Resource.String.loginHintPassword); myUserPassword.SetOnKeyListener(new LoginTextAction(this)); myUserReTypePassword = FindViewById<EditText>(Resource.Id.passwordUserReType); myUserReTypePassword.SetOnKeyListener(new LoginTextAction(this)); buttonRegister = FindViewById<Button>(Resource.Id.btnRegister); buttonRegister.SetOnClickListener(new LoginAction(this)); buttonLogin = FindViewById<Button>(Resource.Id.btnLogin); buttonLogin.SetOnClickListener(new LoginAction(this)); } #endregion } class LoginTextAction : Java.Lang.Object, Android.Views.View.IOnKeyListener { public LoginTextAction(Context Context) { } public bool OnKey (View v, Keycode keyCode, KeyEvent e) { return false; } } class LoginAction : Java.Lang.Object, Android.Views.View.IOnClickListener { public LoginAction(Context Context) { } public void OnClick(View view) { } }

    I set your Program as MainLauncher because i have only this activity.

    Btw. in the .axml you can set the hint attribute with android:hint="puffin". For me it didn't work but there is the possibility. I think you want to set this hint property programatically, right?

  • juanacostajuanacosta AUMember

    Txs Cortez for your help. I discovered that the screen background color was hiding the hint. Unfortunately, when I was working in Visual Studio the render of screens in not so nice and also is slow.
    I check the application in Xamarin Studio and I found the issue really easy.
    I only have to modify the property android:textColorHint="#000000" in the layout.
    Also, I didn't have to modified programmatically only in the layout works fine.
    I think for everyone out there, the lesson is always try in both IDEs before.
    Regards,
    Juan

  • VenkadeshPVenkadeshP USMember

    Thanks juan. I really tried all the ways to get it work. After tried as you said it was working fine. Thanks for saving my time. When you used Material Theme we have to follow the below code

  • MilanRawalMilanRawal USMember

    Out of luck android:hint not working for editText widget in android designer, the xml file is below, and I'm using visual studio 15 community edition with xamarin.android installed. Really this is totally irritating, am I missing something or it's bug with the products.

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="#DEDEDC" android:layout_width="fill_parent" android:layout_height="fill_parent" android:minWidth="300dp" android:minHeight="400dp"> <EditText android:id="@+id/txtFirstName" android:layout_width="match_parent" android:layout_height="35dp" android:background="@drawable/edit_text_style" android:layout_marginBottom="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="25dp" android:hint="First Name" android:paddingLeft="10dp" android:textColor="#000" /> <EditText android:layout_below="@id/txtFirstName" android:inputType="textEmailAddress" android:layout_width="match_parent" android:layout_height="35dp" android:id="@+id/txtEmail" android:background="@drawable/edit_text_style" android:layout_marginBottom="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:hint="Email" android:paddingLeft="10dp" android:textColor="#000" /> <EditText android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="35dp" android:id="@+id/txtPassword" android:background="@drawable/edit_text_style" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:hint="Password" android:paddingLeft="10dp" android:textColor="#000" android:layout_below="@id/txtEmail" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/txtPassword" android:layout_marginTop="50dp"> <Button android:text="Sign Up!" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/btnDialogEmail" android:textSize="20dp" android:textColor="#fff" android:textStyle="bold" android:paddingLeft="5dp" android:paddingBottom="5dp" android:paddingRight="5dp" android:height="70dp" android:paddingTop="2dp" android:background="@drawable/ButtonSignUpStyle" android:layout_centerHorizontal="true" android:layout_marginLeft="30dp" /> </RelativeLayout> </RelativeLayout>

  • YkshLeoYkshLeo USMember ✭✭✭✭

    @MilanRawal, I didn't see any problem with your layouts. Your .axml works perfectly. (I used my own custom background for EditText)

    I think your problem was due to background color of your EditText which will be similar to that of that Hint color.

  • MilanRawalMilanRawal USMember

    @YkshLeo Thanks ! your wright actually the issue was caused by background property of EditText.

  • ObiAzubikeObiAzubike USMember

    How Do I Change the Hint Color @MilanRawal?

Sign In or Register to comment.