How to change line color in EditText

I create an EditText in my xml layout file

But I want to change the color line in the EditText from Holo to (for example) red. How can I do that?

enter image description here

+170
android xml layout styles
Jul 10 '14 at 13:08
source share
20 answers

This is the best tool you can use for all kinds and for FREE thanks to @ Jérôme Van Der Linden .

The Holo Color Generator for Android makes it easy to create Android components like EditText or Spinner, with native colors for the Android app. It will generate all the necessary nine patches, as well as the corresponding XML elements and styles, which you can copy directly into your project.

http://android-holo-colors.com/

UPDATE 1

This domain has expired, but you can find the open source project here

https://github.com/jeromevdl/android-holo-colors

try

this image is placed in the background of an EditText

 android:background="@drawable/textfield_activated" 

enter image description here




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 AppCompatEditText with back support

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" /> 
+258
Jul 10 '14 at 13:23
source share

I do not like the previous answers. The best solution is to use:

 <android.support.v7.widget.AppCompatEditText app:backgroundTint="@color/blue_gray_light" /> 

android: backgroundTint for EditText only works on API21 + . Because of this, we must use the support library and AppCompatEditText .

Note: we must use app: backgroundTint instead of android: backgroundTint

+178
Jan 24 '17 at 11:49 on
source share

You can also quickly change the color of the EditText underline by changing the background color of the EditText as follows:

 <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Something or Other" android:backgroundTint="@android:color/holo_green_light" /> 
+69
Nov 30 '15 at 17:10
source share

Programmatically, you can try:

 editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_ATOP); 
+19
Jul 07 '17 at 4:08 on
source share

for APIs below 21 you can use the theme attribute in edittext, put the code below in the 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 edit text 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" /> 
+15
Jan 31 '18 at 11:08
source share

I think the best way is on the topic:

 <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="addressbookitem_edit_style" 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> <android.support.v7.widget.AppCompatEditText style="@style/addressbookitem_edit_style"/> 
+11
Jul 21 '17 at 20:31
source share

To change the underline color of Edittexts:

If you want the whole app to share this style, you can take the next path.

(1) go to styles.xml file. Your AppTheme, which inherits the parent of Theme.AppCompat.Light.DarkActionBar (in my case), will be the main parent of all the style files for your application. Change its name to "AppBaseTheme". Make another style under it that has the name AppTheme and inherits from the AppBaseTheme that you just edited. It will look like this:

 <!-- 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 color you want with the color of the EditText line.

(2) If you have other folders with style.xml options, this step is very important. Since this file is inherited from your previous parent xml file. For example, I have values ​​-19 / styles.xml. This is specifically for Kitkat and above. Change the parent to AppBaseTheme and make sure that you get rid of "colorAccent" so that it does not override the color of the parents. You also need to save the elements 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> 
+9
May 01 '15 at
source share

It's quite simple (required: at least 21 APIs) ...

  1. Go to your XML and select the EditText field
  2. On the right side you can see the Attributes window. Select "View All Attributes"
  3. Just find the "shade"
  4. And add / change 'backgroundTint' to the desired color hex (say # FF0000)

enter image description here

enter image description here

Keep coding ........ :)

+8
Jun 30 '18 at 11:12
source share

The color of the line is determined by the background property of the EditText . To change it, you have to change android:background in the layout file.

I should note that this style is achieved through the use of a 9-patch. If you look in the SDK, you will see that the background of the EditText is an image:

textfield_activated_holo_light.9.png

To change it, you can open it in the image processing program and paint it in the desired color. Save it as bg_edit_text.9.png and then put it in your folder. Now you can apply it as a background for your EditText as follows:

 android:background="@drawable/bg_edit_text" 
+6
Jul 10 '14 at 13:19
source share

The background of widgets depends on the API level .

ALTERNATIVE 1

You can create your own image on the background of an EditText on

 android:background="@drawable/custom_editText" 

Your image should look something like this. This will give you the desired effect.

enter image description here

ALTERNATIVE 2

Set this xml to your background EditText 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 look the same as your EditText for each API.

+4
Jul 10 '14 at 13:14
source share

You can change the color of the EditText programmatically using this line of code: edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));

+4
Oct 17 '17 at 6:57
source share

You can change the color with the background tinted <EditText android:backgroundTint="@color/red"/>

+4
Oct 17 '17 at 7:09 on
source share

If you need a flat line, you can do it easily with xml. Here is an xml example:

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:top="-1dp" android:left="-1dp" android:right="-1dp" android:bottom="1dp" > <shape android:shape="rectangle"> <stroke android:width="1dp" android:color="#6A9A3A"/> </shape> </item> </layer-list> 

Replace the shape with a selector if you want to provide different widths and colors for the focused edittext.

+2
Dec 07 '15 at 11:32
source share

Use this method .. and change it to match the ur name names. This code works great.

  private boolean validateMobilenumber() { if (mobilenumber.getText().toString().trim().isEmpty() || mobilenumber.getText().toString().length() < 10) { input_layout_mobilenumber.setErrorEnabled(true); input_layout_mobilenumber.setError(getString(R.string.err_msg_mobilenumber)); // requestFocus(mobilenumber); return false; } else { input_layout_mobilenumber.setError(null); input_layout_mobilenumber.setErrorEnabled(false); mobilenumber.setBackground(mobilenumber.getBackground().getConstantState().newDrawable()); } 
+2
Oct 25 '16 at 13:43
source share

A better approach is to use the AppCompatEditText attribute with backgroundTint in the app namespace. i.e.

  <android.support.v7.widget.AppCompatEditText android:layout_width="match_parent" app:backgroundTint="YOUR COLOR" android:layout_height="wrap_content" /> 

when we use android:backgroundTint , it will only work in API21 or more, but app:backgroundTint works at all API levels that your application does.

+2
Mar 12 '17 at 3:16
source share

Use the android: background property for this edittext. Transfer the image with its accessible folder. For example,

 android:background="@drawable/abc.png" 
+1
Jul 10 '14 at 13:30
source share
  <EditText android:id="@+id/et_password_tlay" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:textColorHint="#9e9e9e" android:backgroundTint="#000" android:singleLine="true" android:drawableTint="#FF4081" android:paddingTop="25dp" android:textColor="#000" android:paddingBottom="5dp" android:inputType="textPassword"/> <View android:id="@+id/UnderLine" android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@+id/et_password_tlay" android:layout_centerHorizontal="true" android:background="#03f94e" /> 

** This is one of the views **

+1
Apr 22 '17 at 11:28
source share

Try the following path: it converts the bottom color of the EditText text when used as a background property.

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:left="@dimen/spacing_neg" android:right="@dimen/spacing_neg" android:top="@dimen/spacing_neg"> <shape> <solid android:color="@android:color/transparent" /> <stroke android:width="@dimen/spacing_1" android:color="@android:color/black" /> </shape> </item> </layer-list> 
+1
Jun 14 '17 at 13:56 on
source share

draw /bg_edittext.xml

 <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:gravity="bottom"> <shape> <size android:height="1dp" /> <solid android:color="@android:color/black" /> </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"/> 
+1
May 21 '19 at 5:02
source share

You can simply do this by including this android:theme="@style/AppTheme.AppBarOverlay as an attribute for your editText and adding this <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/> in your styles

0
Jan 05 '18 at 20:15
source share



All Articles