Wednesday, August 19, 2015

How to change EditText hint color in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#eae4e2"
    >
    <!--
        android:textColorHint
            Color of the hint text.

            May be a reference to another resource, in the form
            "@[+][package:]type:name" or to a theme attribute
            in the form "?[package:][type:]name".

            May be a color value, in the form of
            "#rgb", "#argb", "#rrggbb", or "#aarrggbb".
    -->
    <EditText
        android:id="@+id/et"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:hint="Default hint color"
        />
    <EditText
        android:id="@+id/et2"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:hint="Hint color red"
        android:padding="10dp"
        android:layout_below="@+id/et"
        android:textColorHint="#ff0000"
        />
    <EditText
        android:id="@+id/et3"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_below="@+id/et2"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.widget.EditText;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the widgets reference from XML layout
        EditText et = (EditText) findViewById(R.id.et);
        EditText et2 = (EditText) findViewById(R.id.et2);
        EditText et3 = (EditText) findViewById(R.id.et3);

        /*
            setHintTextColor (int color)
                Sets the color of the hint text for all the states
                (disabled, focused, selected...) of this EditText.
         */
        et3.setHintTextColor(Color.BLUE);

        // Set the initial instructional text of third EditText
        // Set hint of the third EditText
        et3.setHint("Hint color blue programmatically");
    }
}
More android examples