How to change the underline color of a TextView in Android?

2

I'm adding the underline to TextView like this:

TextView register = (TextView)findViewById(R.id.txt_register);
SpannableString content = new SpannableString(getString(R.string.text));
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
register.setText(content);

What I add the underline of the same color of TextView , there will be some way to change the color of the underline.

    
asked by A. Palacio 06.06.2018 в 01:23
source

2 answers

1

Searching Stack Overflow in English I found the answer to what I wanted answered by the user Bojan Kseneman

First, custom attributes are defined in the styles.xml file

<declare-styleable name="UnderlinedTextView" >
<attr name="underlineWidth" format="dimension" />
<attr name="underlineColor" format="color" />
</declare-styleable>

Create a custom class for TextView

public class UnderlinedTextView extends AppCompatTextView {
    private Rect mRect;
    private Paint mPaint;
    private float mStrokeWidth;

    public UnderlinedTextView(Context context) {
        this(context, null, 0);
    }

    public UnderlinedTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UnderlinedTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attributeSet, int defStyle) {

        float density = context.getResources().getDisplayMetrics().density;

        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.UnderlinedTextView, defStyle, 0);
        int underlineColor = typedArray.getColor(R.styleable.UnderlinedTextView_underlineColor, 0xFFFF0000);
        mStrokeWidth = typedArray.getDimension(R.styleable.UnderlinedTextView_underlineWidth, density * 2);
        typedArray.recycle();

        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(underlineColor);
        mPaint.setStrokeWidth(mStrokeWidth);
    }

    public int getUnderLineColor() {
        return mPaint.getColor();
    }

    public void setUnderLineColor(int mColor) {
        mPaint.setColor(mColor);
        invalidate();
    }

    public float getUnderlineWidth() {
        return mStrokeWidth;
    }

    public void setUnderlineWidth(float mStrokeWidth) {
        this.mStrokeWidth = mStrokeWidth;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        int count = getLineCount();

        final Layout layout = getLayout();
        float x_start, x_stop, x_diff;
        int firstCharInLine, lastCharInLine;

        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, mRect);
            firstCharInLine = layout.getLineStart(i);
            lastCharInLine = layout.getLineEnd(i);

            x_start = layout.getPrimaryHorizontal(firstCharInLine);
            x_diff = layout.getPrimaryHorizontal(firstCharInLine + 1) - x_start;
            x_stop = layout.getPrimaryHorizontal(lastCharInLine - 1) + x_diff;

            canvas.drawLine(x_start, baseline + mStrokeWidth, x_stop, baseline + mStrokeWidth, mPaint);
        }
        super.onDraw(canvas);
    }
}

Use of the class in the Layout:

<some.package.UnderlinedTextView
    android:id="@+id/tvTest"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:gravity="center"
    android:text="This is a demo text"
    android:textSize="16sp"
    app:underlineColor="#ffc112ef"
    app:underlineWidth="3dp"/>

Result example:

    
answered by 06.06.2018 / 22:31
source
0

Create a UndelineSpan but define another color:

   UnderlineSpan underlineSpan = new UnderlineSpan();
   TextPaint tp = new TextPaint();
   tp.setColor(Color.GREEN);  //*Color verde!
   underlineSpan.updateDrawState(tp);

Example:

    TextView textView = (TextView)findViewById(R.id.textView);
    SpannableString content = new SpannableString("Jorgesys was here! ce faci mama!");

    UnderlineSpan underlineSpan = new UnderlineSpan();
    TextPaint tp = new TextPaint();
    tp.setColor(Color.GREEN);
    underlineSpan.updateDrawState(tp);

    content.setSpan(underlineSpan, 0, content.length(), 0);
    textView.setText(content);
    
answered by 06.06.2018 в 01:41