Is there any way to change Underline distance of word in a paragraph in textbox in android?

36 Views Asked by At

I am trying to change distance between the underline and text of a word in a paragraph in Text Box, the issue I face is, when I try to add code for distance height, color span was not working, when I try to add color span distance, was not working. I need both. How can I achieve this?(https://i.stack.imgur.com/QQh1W.png)

Currently I am using this code to draw a underline with custom color, thickness and distance between text and underline

public class CustomUnderlinedTextView extends AppCompatTextView {

private Paint underlinePaint;

public CustomUnderlinedTextView(Context context) {
    super(context);
    init();
}

public CustomUnderlinedTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

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

private void init() {
    int underlineThickness;
    int underlineColor;
    underlineColor = ContextCompat.getColor(getContext(), R.color.orange_new);
    underlineThickness = 3;

    underlinePaint = new Paint();
    underlinePaint.setColor(underlineColor);
    underlinePaint.setStyle(Paint.Style.STROKE);
    underlinePaint.setStrokeWidth(underlineThickness);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int orangeNew = ContextCompat.getColor(getContext(), R.color.orange_new);

    // Create a SpannableStringBuilder for the entire text
    CharSequence text = getText();
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text);

    // Find the index of the word "Privacy Policy" in the text
    String targetText = "Privacy Policy"; // Change to your target text
    int startIndex = text.toString().indexOf(targetText);

    if (startIndex >= 0) {
        int endIndex = startIndex + targetText.length();

        // Apply UnderlineSpan and ForegroundColorSpan to "Privacy Policy"
        spannableStringBuilder.setSpan(new ForegroundColorSpan(orangeNew), startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        // Calculate the position to draw the underline
        Layout layout = getLayout();
        int line = layout.getLineForOffset(startIndex);
        int startX = (int) layout.getPrimaryHorizontal(startIndex);
        int endX = (int) layout.getPrimaryHorizontal(endIndex);
        //Adjust Distance between text and Underline
        int yOffset = (layout.getLineBaseline(line) + 14);

        // Draw the underline
        canvas.drawLine(startX, yOffset, endX, yOffset, underlinePaint);
    }

    // Set the modified SpannableStringBuilder as the text
    setText(spannableStringBuilder);
}

}

My problem is, for Example "Privacy policy" is the word which is required to be modified, when the word warp as Privacy Policy in separate lines, the underline was working for Privacy alone which extends underline to empty space. But "Policy" doesn't have any underline. How can I fix this? And also, after a couple of minutes that screen started to drop frames, which looks laggy, is there any way to fix this? I am currently using Galaxy Note 10 Lite for testing. Thankyou in Advance!

0

There are 0 best solutions below