Search code examples
javaandroidunderlinespannablestring

Dotted underline in TextView using SpannableString in Android


I want dotted underline below SpannableString Like here is my string

Hello, How are you?

I want to develop dotted underline below How word so how to set it? If i add clickable that its give me underline but i want dotted underline below How word. Can anyone help me ? Actually on click of that How word i am opening a dialog so that thing i achieved with clickable but i want to design that How word with dotted underline.

Editted: Using below post dotted underline appears but text color is not showing using ForegroundColorSpan class. can anybody help me to resolve this issue.


Solution

  • You should use two SpannableString for it as

    SpannableString ss = new SpannableString("Hello, how are you ?");
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
             //perform operation on click
    
            }
    
            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
            }
        };
        ss.setSpan(clickableSpan, 0, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ForegroundColorSpan(getActivity().getResources().getColor(android.R.color.holo_blue_light)), 0, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView_understand.setText(ss);
        textView_understand.setMovementMethod(LinkMovementMethod.getInstance());
        textView_understand.setHighlightColor(Color.TRANSPARENT);
        textView_understand
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    //On click
                    }
                });
    

    And for make it dotted underline you should use

    private static class DottedUnderlineSpan extends ReplacementSpan {
    private Paint p;
    private int mWidth;
    private String mSpan;
    
    private float mSpanLength;
    private boolean mLengthIsCached = false;
    
    public DottedUnderlineSpan(int _color, String _spannedText){
        p = new Paint();
        p.setColor(_color);
        p.setStyle(Paint.Style.STROKE);
        p.setPathEffect(new DashPathEffect(new float[]{mDashPathEffect, mDashPathEffect}, 0));
        p.setStrokeWidth(mStrokeWidth);
        mSpan = _spannedText;
    }
    
    @Override
    public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
        mWidth = (int) paint.measureText(text, start, end);
        return mWidth;
    }
    
    @Override
    public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
        canvas.drawText(text, start, end, x, y, paint);
        if(!mLengthIsCached)
            mSpanLength = paint.measureText(mSpan);
    
        // https://code.google.com/p/android/issues/detail?id=29944
        // canvas.drawLine can't draw dashes when hardware acceleration is enabled,
        // but canvas.drawPath can
        Path path = new Path();
        path.moveTo(x, y + mOffsetY);
        path.lineTo(x + mSpanLength, y + mOffsetY);
        canvas.drawPath(path, this.p);
    }
    }
    

    and use this class for span as:

      DottedUnderlineSpan dottedUnderlineSpan = new DottedUnderlineSpan(0xFF00FF00, spannedText);
    

    To make your underline look the same on all densities set that dimens in dp

    mStrokeWidth = context.getResources().getDimension(R.dimen.stroke_width);
    mDashPathEffect = context.getResources().getDimension(R.dimen.dash_path_effect);
    mOffsetY = context.getResources().getDimension(R.dimen.offset_y);