Android – Programatically changing text color of EditText in EditorAction listener

January 22, 2013
By

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"?>
<resources>
    <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>
</resources>

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <EditText
        android:id="@+id/et_color"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:hint="@string/hnt_color"
        android:inputType="text" />

</RelativeLayout>


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


package in.wptrafficanalyzer.textcoloredittextprogramatically;

import android.app.Activity;
import android.graphics.Color;
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;

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

        // Getting reference to EditText
        etColor = (EditText) findViewById(R.id.et_color);

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

                        // 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;
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, 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 wptrafficanalyzer.in

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

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


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