Androider
Androider

Reputation: 417

Android add underline dynamically

I watn to dynamically add and remove unerline for a TextView in my application.

Firstly I am adding underline in onCreateView of a fragment with using of AQuery: aQuery.id(R.id.tv_help_consult).getTextView().setPaintFlags(aq.id(R.id.tv_help_consult).getTextView().getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

And it looks fine.
enter image description here

But when I am removing underline (and adding again) something is happening to the font so you can see pixels:

enter image description here

I am using these methods in a clickListenner to change the underline:

private void headingFontHighlightOn(TextView heading) {
        //        removing underline
        heading.setPaintFlags(0);
        //        changing color
        heading.setTextColor(ContextCompat.getColor(getActivity(), R.color.orange));
    }


private void headingFontHighlightOff(TextView heading) {
         //        making underline
         heading.setPaintFlags(heading.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
         //        changing color
         heading.setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
        }

Of course, I can use alternative String resource with <u> tags and just change the string resource.
But I want to firure out what is happenning here

EDIT: in case somebody needs,
My final solution is based on Chintan Bawa's answer.
I just modified methods to be independent from String resources (while using on different TextViews):

private void headingFontHighlightOn(TextView heading) {
        //        removing underline
        heading.setText(heading.getText().toString());
        //        changing color
        heading.setTextColor(ContextCompat.getColor(getActivity(), R.color.orange));
    }

    private void headingFontHighlightOff(TextView heading) {
        //        making underline
        String underlinedText = "<u>" + heading.getText() + "</u>";
        heading.setText(Html.fromHtml(underlinedText));

        //        changing color
        heading.setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
    }

Also I removed all paint flags.

Upvotes: 1

Views: 597

Answers (2)

Jotiram Chavan
Jotiram Chavan

Reputation: 375

You can use clickable span if you want that underline should be clickable.If not just ignore onclick operations.You can use below code to have underline how you like

String str = "You have " + sessionUserId.getfudiyoPointsSession() + " fudiyo points available. What are fudiyo points?";

        int index = str.indexOf(".");
        TextView txtfudiyo_point = (TextView) findViewById(R.id.user_fudiyo_point);

        SpannableString ss = new SpannableString(str);
        ClickableSpan clickableSpan = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                try {
                    showTermsConditions();
                } catch (Exception e) {
                    ErrorEmail.sendEmail(UserHomeScreen.this, e);
                }
            }

            @Override
            public void updateDrawState(TextPaint ds) {

                super.updateDrawState(ds);
                ds.setColor(Color.RED);
                ds.setUnderlineText(true);
            }
        };
        ss.setSpan(clickableSpan, index + 2, ss.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        txtfudiyo_point.setText(ss);
        txtfudiyo_point.setMovementMethod(LinkMovementMethod.getInstance());

Upvotes: 0

Chintan Bawa
Chintan Bawa

Reputation: 1386

You can use this to underline text in TextView

heading.setText(Html.fromHtml("<u>Legal assistance</u>"));

and to remove underline from text in TextView

heading.setText("Legal assistance"));

Upvotes: 3

Related Questions