Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, change underline Color from an EditText dynamically

is it possible to change the color of the underline from a EditText dynamically? Like the effect, when i focus it, then it turns into blue.

Hope you understand what i want to do.

like image 820
R.Z Avatar asked Nov 05 '12 18:11

R.Z


People also ask

How do I change the line color in TextInputLayout?

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 .


2 Answers

Create your own customized EditText control.

Here's an example that I made just for you:

When selected, you just have to change mPaint.setColor(Color.GREEN); to another color

public class CustomEditText extends EditText {
    private Rect mRect;
    private Paint mPaint;
    int widthMsSize;
    int heightMsSize;
    // we need this constructor for LayoutInflater
    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setColor(Color.GREEN);
        System.out.println("constructor");
    }
    protected void onMeasure(final int widthMeasureSpec,
        final int heightMeasureSpec) {
        // Extract the Ms (MesaureSpec) parameters
        widthMsSize = MeasureSpec.getSize(widthMeasureSpec);
        heightMsSize = MeasureSpec.getSize(heightMeasureSpec);
        System.out.println("on measure");
        // Satisfy contract by calling setMeasuredDimension
        setMeasuredDimension(widthMsSize,
            heightMsSize);
    }
    protected void onDraw(Canvas canvas) {
        canvas.drawLine(5, heightMsSize - 10, widthMsSize - 5, heightMsSize - 10, mPaint); //draw underline
        canvas.drawLine(8, heightMsSize - 10, 8, heightMsSize - 20, mPaint); //draw left corner
        canvas.drawLine(widthMsSize - 8, heightMsSize - 10, widthMsSize - 8, heightMsSize - 20, mPaint); //draw right corner
        super.onDraw(canvas);
    }
}

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <com.example.testanimationfadeinfadeout.CustomEditText
        android:id="@+id/textedit"
        android:layout_width="228dp"
        android:layout_height="41dp"
        android:ems="10"
        android:hint="color is changed" />

</LinearLayout>
like image 154
Frank Avatar answered Oct 14 '22 08:10

Frank


public static void setEditTextUnderlineColor(final EditText editText, final int focusedColor, final int unfocusedColor) {
    editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                editText.getBackground().setColorFilter(focusedColor, PorterDuff.Mode.SRC_ATOP);
                return;
            }
            editText.getBackground().setColorFilter(unfocusedColor, PorterDuff.Mode.SRC_ATOP);
        }
    });

    editText.getBackground().setColorFilter(unfocusedColor, PorterDuff.Mode.SRC_ATOP);
like image 22
SIr Codealot Avatar answered Oct 14 '22 09:10

SIr Codealot