2

Estoy agregando el subrayado a un TextView de esta manera:

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);

Lo que me agrega el subrayado del mismo color del TextView, habrá alguna forma de cambiar el color del subrayado.

2 respuestas 2

1

Buscando en Stack Overflow en ingles encontre la respuesta a lo que queria respondida por el usuario Bojan Kseneman

Primero se definen los atributos personalizados en el archivo styles.xml

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

Crear una clase personalizada para 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);
    }
}

Uso de la clase en el 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"/>

Resultado ejemplo:

Resultado

0

Crea un UndelineSpan pero definiendo otro color:

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

Ejemplo:

    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);
1
  • Gracias por responder, pero aun así me sigue mostrando el mismo color del TextView.
    – A. Palacio
    el 6 jun. 2018 a las 15:01

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.