Henry
Henry

Reputation: 496

Changing the focus colour of a EditText?

How can I change the colour of a EditText when focused? In the emulator it is blue when focused, and I can't see a way to change it from either an XML layout or programmatically. I can't figure out how to change the focus colour of other View as well like Buttons and Spinners. The selected colour of the EditText blue

Upvotes: 4

Views: 5299

Answers (1)

Androyds
Androyds

Reputation: 491

yourEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

                public void onFocusChange(View v, boolean hasFocus) {
                    if (v == yourEditText)
                    {
                        if (hasFocus)
                        {
                           yourEditText..setTextColor(Color.RED);

                        } 
                        else
                        { 

                        }
                    }

                }
            });

use focus listener for that (button etc)

EDIT You have to use XML and set the background of the view for that

   <?xml version="1.0" encoding="utf-8"?>
<selector 
xmlns:android="http://schemas.android.com/apk/res/android"
>
    <item 
      android:state_pressed="true"
      android:color="yourDesiredcolorcode" 
    /> <!-- pressed -->    
    <item 
       android:state_focused="true"
       android:color="yourDesiredcolorcode"
    /> <!-- focused -->    
    <item 
           android:color="yourDesiredcolorcode"
    /> <!-- default -->
</selector>

Cheers

Upvotes: 1

Related Questions