Tuesday, October 4, 2016

android - EditText hint color

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    >
    <!--
        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".
            This corresponds to the global attribute resource symbol textColorHint.
    -->
    <EditText
        android:id="@+id/et"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:textSize="25sp"
        android:padding="5dp"
        android:layout_marginTop="50dp"
        android:hint="Write some text here."
        android:textColorHint="#ff0000"
        />
</android.support.design.widget.CoordinatorLayout>

2. Change EditText hint color programmatically

MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private EditText mEditText;


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

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mEditText = (EditText) findViewById(R.id.et);

        /*
            setHintTextColor
                void setHintTextColor (int color)

                Sets the color of the hint text for all the
                states (disabled, focussed, selected...) of this TextView.
        */

        // Change edit text hint color programmatically
        mEditText.setHintTextColor(Color.GREEN);
    }
}