Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android edit text change background in focus

I have used edit text in my app and I want to change the background when the edit text is focused. I wrote some code but I am facing a problem. I need twice click in edit text to show keyboard.

This is my code:

  private View.OnFocusChangeListener myEditTextFocus =  new View.OnFocusChangeListener() {
    public void onFocusChange(View view, boolean hasfocus) {
        if (hasfocus) {
            ((EditText) view).setBackgroundResource(R.drawable.edittext_input_background_focus);

            ((EditText) view).setTextColor(Color.parseColor("#4d4d4d"));

        }
        else {
            ((EditText) view).setBackgroundResource(R.drawable.edittext_input_background_not_focus);

        }
    };
};

The problem is this code because I commented it and everything was perfect.What is wrong with my code? Or, Is there any other solution?

like image 695
donoachua Avatar asked Nov 03 '15 06:11

donoachua


People also ask

How do I change the text color on focus in Android?

How do I change the text color on focus in Android? For changing the baseline of EditText when it is on focus you can simply go to the colors. xml file in your project and change the "colorAccent" value.

What is EditText in Android?

A EditText is an overlay over TextView that configures itself to be editable. It is the predefined subclass of TextView that includes rich editing capabilities.


1 Answers

//Follow this code

Put these 3 drawable files in res/drawable folder

simple_edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >

    <solid android:color="#FFFFFF" />

    <stroke
        android:width="3dp"
        android:color="#FB9820" />

    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />

</shape>

focus_edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="10dp"
    android:shape="rectangle" >
    <solid android:color="#58ACFA" />

    <stroke
        android:width="3dp"
        android:color="#FB9820" />
    <corners
        android:bottomLeftRadius="5dp"
        android:bottomRightRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp" />
</shape>

selector_edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/focus_edittext"/>
    <item android:drawable="@drawable/simple_edittext" />
</selector>

And use these drawbles like this :

 <EditText
        android:id="@+id/layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/selector_edittext"
        android:text="@string/hello_world" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout1"
        android:padding="10dp"
        android:background="@drawable/selector_edittext"
        android:text="@string/hello_world" />
like image 75
Chirag Savsani Avatar answered Oct 17 '22 02:10

Chirag Savsani