Change background color of edittext in android
Solution 1
What you should do is to create a 9 patch image for edittext and set that image as edit text background. You can create 9 patches using this website
I am attaching a sample 9 patch image for your reference.Use it as edittext background and you will get an idea.Right click the image and select "save image as". When you save the image dont forget to give its extension as "9.png"
Solution 2
one line of lazy code:
mEditText.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
Solution 3
Here the best way
First : make new xml
file in res
/drawable
name it rounded_edit_text
then paste this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#F9966B" />
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
</shape>
Second: in res/layout copy and past following code (code of EditText
)
<EditText
android:id="@+id/txtdoctor"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="@drawable/rounded_edit_text"
android:ems="10" >
<requestFocus />
</EditText>
Solution 4
I create color.xml file, for naming my color name (black, white...)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
<color name="black">#000000</color>
</resources>
And in your EditText, set color
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdsadasdasd"
android:textColor="@color/black"
android:background="@color/white"
/>
or use style in you style.xml:
<style name="EditTextStyleWhite" parent="android:style/Widget.EditText">
<item name="android:textColor">@color/black</item>
<item name="android:background">@color/white</item>
</style>
and add ctreated style to EditText:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdsadasdasd"
style="@style/EditTextStyleWhite"
/>
Solution 5
The simplest solution I have found is to change the background color programmatically. This does not require dealing with any 9-patch images:
((EditText) findViewById(R.id.id_nick_name)).getBackground()
.setColorFilter(Color.<your-desired-color>, PorterDuff.Mode.MULTIPLY);
Source: another answer
Naruto
Updated on August 24, 2020Comments
-
Naruto over 3 years
If I change the background color of my
EditText
using the below code, it looks like the box is shrunken and it doesn't maintain the ICS theme of a blue bottom border that exists for a defaultEditText
.<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#99000000" > <EditText android:id="@+id/id_nick_name" android:layout_marginTop="80dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#ffffff" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:orientation="horizontal" android:layout_below="@+id/id_nick_name"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="add" android:layout_weight="1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="cancel" android:layout_weight="1" /> </LinearLayout> </RelativeLayout>
Here is what it looks like:
-
Naruto over 10 yearscool, it works like as i expected only thing is. i just need to change the image as per my requirement. thanks
-
Mohamed Ibrahim over 10 yearsyou can change solid color to the color you need
-
Barrie Galitzky over 9 yearsyou're missing .getBackground() ((EditText) findViewById(R.id.id_nick_name)).getBackground().setColorFilter(Color.<your-desired-color>, PorterDuff.Mode.MULTIPLY);
-
Tom over 9 yearsThis works great for tinting the background across both Holo and Material Design and is easy to implement. Thanks! This should probably be the accepted answer - it preserves the original background of the view and its dimensions, while changing its colour.
-
Sergey Maslov over 8 yearsyou are THE lifesaver!
-
Georgian Benetatos over 8 yearsThis works, but the other edittexts in my app keep this color
-
Marc Plano-Lesay over 7 years@GeorgianBenetatos you should mutate the drawable before changing it if you only want to alter a specific instance: android-developers.blogspot.fr/2009/05/drawable-mutations.html
-
Pravinraj Venkatachalam over 7 yearsPlease help me to know, how the layout reads the drawable file ? Thanks in advance.