Android Different Edittext Backgrounds for Different States Using Shapes, Selector or List

Android different EditText backgrounds for different states using shapes, selector or list

I suggest to use NinePatch images to customize your EditText. Here goes an example based on my code:

The Selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/twitter_im_edittext_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/twitter_im_edittext_normal" />
<item android:state_pressed="true" android:drawable="@drawable/twitter_im_edittext_normal" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/twitter_im_edittext_focused" />
<item android:state_enabled="true" android:drawable="@drawable/twitter_im_edittext_normal" />
<item android:state_focused="true" android:drawable="@drawable/twitter_im_edittext_focused" />
<item android:drawable="@drawable/twitter_im_edittext_normal" />
</selector>

Use selector the same way you used in your code, set it to background of your EditText.

Images:

twitter_im_edittext_focused.9.png
twitter_im_edittext_focused.9.png

twitter_im_edittext_normal.9.png
twitter_im_edittext_normal.9.png

More about NinePatch images you can found here.

Hope it helps.

How to change EditText background without code changes, only xml?

edit_background,.xml

<selector xmlns:android=”http://schemas.android.com/apk/res/android”>

<item android:drawable=”@drawable/edittext_normal”
android:state_enabled=”true” android:state_window_focused=”false”/>
<item android:drawable=”@drawable/edittext_normal”

android:state_enabled=”false” android:state_window_focused=”false”/>
<item android:drawable=”@drawable/edittext_press”

android:state_enabled=”true” android:state_focused=”true”/>
<item android:drawable=”@drawable/edittext_normal”

android:state_enabled=”true”/>
<item android:drawable=”@drawable/edittext_press”

android:state_focused=”true”/>
<item android:drawable=”@drawable/edittext_normal”/>

</selector>

edittext_normal.vml

 <?xml version=”1.0″ encoding=”utf-8″?>
<layer-list xmlns:android=”http://schemas.android.com/apk/res/android” >

<!– “background shadow” –>
<item>
<shape android:shape=”rectangle” >
<solid android:color=”#ffffffff” />

<stroke
android:width=”1dp”
android:color=”#e3e3e3″ />
</shape>
</item>

</layer-list>

edittext_press.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<layer-list xmlns:android=”http://schemas.android.com/apk/res/android” >

<item>
<shape android:shape=”rectangle” >
<solid android:color=”@color/progress_blue” />
</shape>
</item>
<item
android:bottom=”0dp”
android:left=”0dp”
android:right=”0dp”
android:top=”0dp”>
<shape android:shape=”rectangle” >
<solid android:color=”@color/progress_blue” />
</shape>
</item>
<item
android:bottom=”2dp”
android:left=”2dp”
android:right=”2dp”
android:top=”2dp”>
<shape android:shape=”rectangle” >
<solid android:color=”@color/textwhite” />
</shape>
</item>

</layer-list>

And your Edittext

<EditText
android:id=”@+id/edttext”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:background=”@drawable/editbackground”
android:textSize=”15dp” />

Android EditText with active state

You can achieve this effect in many ways, but most likely simplest would be to create drawable (XML) image with <selector> and reference different images depending on the state (you do not need to define all states). Here's documentation (see "State List" chapter), or random SO's question on similar subject (search for more yourself).

Change layout of EditText on state selected

Try this

<item android:state_enabled="true" android:state_focused="true">

EDIT:

Try this and check if the background color is changed when clicked on edittext

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="#f07c01"
/> <!-- pressed -->
<item
android:state_enabled="true" android:state_focused="true"
android:drawable="#c099cc00"
/> <!-- focused -->
<item
android:drawable="#0d91a8"
/> <!-- default -->
</selector>

Android EditText native selector

Since no one was able to completely answer my question I will answer it by myself. The problem was that my edittext became smaller every time I touched it. That happens because native edittext background in Android has a transparent area around it. So I used a layer list to create my background also with a transparent area around it. Here is the code:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/transparent_background"
android:top="5dip"
android:right="5dip"
android:bottom="5dip"
android:left="5dip" />
<item
android:drawable="@drawable/my_border"
android:top="0dip"
android:right="0dip"
android:bottom="0dip"
android:left="0dip" />
</layer-list>

In my selector instead of my_border I use this xml.
That's it. Easy to do and hell difficult to find out how!

when selected add border shape for textView and editText ,

You should use drawable selector to achieve your UI.

First create a background_edit_text_default.xml which is the background of EditText when it is not selected by users.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#333D46" />

<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>

Then create a background_edit_text_selected.xml which is the background of EditText when it is selected by users.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="1dp"
android:color="#EDB90E" />

<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>

Next create a background_edit_text.xml which will be used as background of the EditText.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/background_edit_text_default" android:state_focused="false" />
<item android:drawable="@drawable/background_edit_text_selected" android:state_focused="true" />

</selector>

Finally set background_edit_text.xml as background of your EditText in your layout file, such as activity_main.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/conteiner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_edit_text" />

<TextView
android:layout_width="match_parent"
android:layout_height="10dp" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_edit_text" />

</LinearLayout>

You're done and no need to add anything in code.

Change drawable when a field gains focus

It can be easily done using simple selectors once you create a custom drawable.

Check this tutorial where it applies what you want to a Button.

http://www.mkyong.com/android/android-imagebutton-selector-example/

Something like this should work for you:

<EditText
style="@style/EditText"
android:id="@+id/email"
android:hint="Email Address"
android:inputType="textEmailAddress"
android:background ="@drawable/customDrawableIcon" />

res/drawable/customDrawableIcon.xml

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


Related Topics



Leave a reply



Submit