shaoping wang
shaoping wang

Reputation: 53

How to change EditText border line color in program code?

Actually I have 2 questions.

  1. How to change edittext border line color and size? Other answers like use shape.xml to describe the rect detail and set android:background of EditText as @drawable/shape does not work for me, because I need to change focus cursor color too. I try to use styles.xml and set colorAccent as the line color, and set EditText android:theme as this. It works for color, but I cannot figure out how to change line size in style.xml.

  2. I want to change the color of EditText at running time, how to change the EditText line color and focus cursor color in program code?

enter image description here

Any help is appreciated! Thanks so much!

I want to change the white line size as 1 dp.

Upvotes: 3

Views: 2678

Answers (2)

Piyush
Piyush

Reputation: 18933

You can use:

For API level 21 or >21

ColorStateList colorStateList = ColorStateList.valueOf(ContextCompat.getColor(this,R.color.red));
editText.setBackgroundTintList(colorStateList);

For API level < 21

editText.getBackground().mutate().setColorFilter(ContextCompat.getColor(this,R.color.red), PorterDuff.Mode.SRC_ATOP);

android:textCursorDrawable attribute to @null will give you the cursor color same as text color of EditText which you have applied.

For border line :

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

Upvotes: 2

Arpan Sharma
Arpan Sharma

Reputation: 2162

editText.getBackground().mutate().setColorFilter(getResources().getColor(R.color.AppColor), PorterDuff.Mode.SRC_ATOP);

also android:textCursorDrawable attribute to @null should result in the use of android:textColor as the cursor color.

Use this code in your java class.

Upvotes: 0

Related Questions