I am creating an EditText in my layout xml file
But I want to change color line in EditText from Holo to (for example) red. How that can be done?
To change the hint color you have to use these attributes: hintTextColor and android:textColorHint . To change the bottom line color you have to use the attribute: boxStrokeColor .
Use this code to filter your edittext private String yourCharacterThatYouWantToBlock= " ~#^|$%&*!"; private InputFilter filter = new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (source !=
This is the best tool that you can use for all views and its FREE many thanks to @Jérôme Van Der Linden.
The Android Holo Colors Generator allows you to easily create Android components such as EditText
or spinner with your own colours for your Android application. It will generate all necessary nine patch assets plus associated XML drawable and styles which you can copy straight into your project.
http://android-holo-colors.com/
UPDATE 1
This domain seems expired but the project is an open source you can find here
https://github.com/jeromevdl/android-holo-colors
try it
this image put in the background of EditText
android:background="@drawable/textfield_activated"
UPDATE 2
For API 21 or higher, you can use android:backgroundTint
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
android:backgroundTint="@android:color/holo_red_light" />
Update 3
Now We have with back support AppCompatEditText
Note: We need to use app:backgroundTint instead of android:backgroundTint
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
app:backgroundTint="@color/blue_gray_light" />
Update 4 AndroidX version
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
I don't like previous answers. The best solution is to use:
<android.support.v7.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
android:backgroundTint
for EditText
works only on API21+ . Because of it, we have to use the support library and AppCompatEditText
.
Note: we have to use app:backgroundTint
instead of android:backgroundTint
AndroidX version
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="@color/blue_gray_light" />
You can also quickly change the EditText's underline color by tinting the background of the EditText like so:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Something or Other"
android:backgroundTint="@android:color/holo_green_light" />
for API below 21, you can use theme attribute in EditText
put below code into style file
<style name="MyEditTextTheme">
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlActivated">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
</style>
use this style in EditText
as
<EditText
android:id="@+id/etPassword"
android:layout_width="match_parent"
android:layout_height="@dimen/user_input_field_height"
android:layout_marginTop="40dp"
android:hint="@string/password_hint"
android:theme="@style/MyEditTextTheme"
android:singleLine="true" />
Programmatically, you can try:
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_ATOP);
Its pretty simple (Required: Minimum API 21)...
Keep Coding........ :)
You can change the color of EditText programmatically just using this line of code:edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));
i think the best way is by theme:
<style name="MyEditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">@color/black</item>
<item name="colorControlActivated">@color/action_blue</item>
<item name="colorControlHighlight">@color/action_blue</item>
</style>
<style name="AddressBookStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">13sp</item>
<item name="android:theme">@style/MyEditTextTheme</item>
</style>
<android.support.v7.widget.AppCompatEditText
style="@style/AddressBookStyle"/>
To change Edittext’s underline color:
If you want the entire app to share this style, then you can do the following way.
(1) go to styles.xml file. Your AppTheme that inherits the parent of Theme.AppCompat.Light.DarkActionBar (in my case) will be the base parent of all they style files in your app. Change the name of it to “AppBaseTheme’. Make another style right under it that has the name of AppTheme and inherits from AppBaseTheme that you just edited. It will look like following:
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<!--see http://www.google.com/design/spec/style/color.html#color-color-palette-->
<item name="colorPrimary">@color/material_brown_500</item>
<item name="colorPrimaryDark">@color/material_brown_700</item>
<item name="colorAccent">@color/flamingo</item>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Customize your theme here. -->
</style>
Then change the “colorAccent” to whatever the color you want your EditText line color to be.
(2) If you have other values folders with style.xml, this step is very important. Because that file will inherit from your previous parent xml file. For example, I have values-19/styles.xml. This is specifically for Kitkat and above. Change its parent to AppBaseTheme and make sure to get rid of “colorAccent” so that it doesn’t override the parent’s color. Also you need to keep the items that are specific to version 19. Then it will look like this.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
The line's color is defined by EditText
's background property. To change it you should change the android:background
in the layout file.
I should note that this style is achieved by using a 9-patch drawable. If you look in the SDK, you can see that the background of the EditText
is this image:
To change it you could open it in an image manipulation program and color it in desired color. Save it as bg_edit_text.9.png
and then put it in you drawable folder. Now you can apply it as a background for your EditText
like so:
android:background="@drawable/bg_edit_text"
drawable/bg_edittext.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="@android:color/transparent" />
</shape>
</item>
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="1dp"
android:color="@color/colorDivider" />
</shape>
</item>
</layer-list>
Set to EditText
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_edittext"/>
The background of widgets are API level dependent.
ALTERNATIVE 1
You can provide a custom image to your EditText
background by
android:background="@drawable/custom_editText"
Your image should look something like this. It will give you the desired effect.
ALTERNATIVE 2
Set this xml to your EditText
background attribute.
<?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="#4C000000"/>
<corners android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
This will have the same look and feel of your EditText
on every API.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With