Monday, April 30, 2012

How to change the hint text color in android?


How to change the hint text color in android?



Hello all..
This simple example will show you how to change the hint text color in android
Here is the java code to simply do this.
?
1
youredittext.setHint(Html.fromHtml("<font color='#FF0000'>Hello</font> "));
here is a sample project to view the difference.
This is the contents of the main java file.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.coderzheaven.pack;
 
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.EditText;
 
public class HintColorDemoActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
 
          EditText ed = (EditText)findViewById(R.id.editText1);
          ed.setHint("Hello ");
 
          EditText ed2 = (EditText)findViewById(R.id.editText2);
          ed2.setHint(Html.fromHtml("<font color='#FF0000'>Hello</font> "));
     }
}
The main.xml file
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     android:orientation="vertical" >
 
     <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello" />
 
     <EditText
          android:id="@+id/editText1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:ems="10" >
 
          <requestFocus />
     </EditText>
 
     <EditText
          android:id="@+id/editText2"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:ems="10" >
     </EditText>
</LinearLayout>

Here I am setting a read color to the second edittext hint.
See the screenshot.
Please leave your comments if you found this useful.

No comments: