Android – Programatically changing text color of EditText in EditorAction listener

January 22, 2013

In this article, we will develop an Android application which changes the text color of an EditText widget when a valid color is entered in the EditText.

This application is developed in Eclipse ( 4.2.1 ) with ADT plugin ( 21.0.0 ) and Android SDK ( 21.0.0 ).

1. Create a new Android application project namely “TextColorEditTextProgramatically”

Creating new Android Application project

Figure 1 : Creating new Android Application project

2. Configure the project

Configuring the Android application project

Figure 2 : Configuring the Android application project

3. Design Application launcher icon

Design the application launcher icon

Figure 3 : Design the application launcher icon

4. Create a blank activity

Create a blank activity

Figure 4 : Create a blank activity

5. Enter MainActivity details

Enter MainActivity details

Figure 5 : Enter MainActivity details

6. Delete Android’s support library from this project, if exists ( Optional Step )

By default Eclipse ( 4.2.1) adds Android Support Library to¬† Android application project. For this application, we don’t need to use this support library. So the library file libs/android-support-v4.jar may be removed manually via ProjectExplorer by simply right click on the file and then clicking the menu item “delete”

7. Update the file res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
    <string name="app_name">TextColorEditTextProgramatically</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="hnt_color">Enter color ( eg : #a2b5cf )</string>

8. Update the file res/layout/activity_main.xml

<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:inputType="text" />


9. Update the file src/in/wptrafficanalyzer/textcoloredittextprogramatically/

package in.wptrafficanalyzer.textcoloredittextprogramatically;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class MainActivity extends Activity {
    EditText etColor;

    protected void onCreate(Bundle savedInstanceState) {

        // Getting reference to EditText
        etColor = (EditText) findViewById(;

        // Setting an EditorActionListener for the EditText
        etColor.setOnEditorActionListener(new OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                // Getting the user input text
                String color = v.getText().toString();
                    // If user presssed Done button of the keyboard
                    if(actionId == EditorInfo.IME_ACTION_DONE){
                        // Changing the color the text

                        // Showing a Toast message
                        Toast.makeText(getBaseContext(), v.getText(), Toast.LENGTH_SHORT).show();
                }catch(IllegalArgumentException e){
                    Toast.makeText(getBaseContext(), "Invalid color. Enter color as eg : #a2fa34 ", Toast.LENGTH_SHORT).show();
                }catch(Exception e){
                    Toast.makeText(getBaseContext(), "Invalid, enter color as eg : #a2fa34 ", Toast.LENGTH_SHORT).show();
                // return false, means , continue to other handlers
                return false;

    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

10. Screenshot of the application in execution

Entering Hexa Decimal value corresponding to a color

Figure 6 : Entering Hexa Decimal value corresponding to a color

Color of the text is changed after entering the color code

Figure 7 : Color of the text is changed after entering the color code

11. Download source code

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner