0

I have a use case when it's needed only a part of text to be clickable and different color,on some screens my text have to be written in 2 lines as:enter image description here

but I get: enter image description here

Using this code:

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageButton
            android:id="@+id/btn_accept_terms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@mipmap/selected" />

        <TextView
            android:id="@+id/terms1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/btn_accept_terms"
            android:text="@string/txt_terms" />

        <TextView
            android:id="@+id/terms2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/terms1"
            android:text="@string/txt_terms_hyperlink"
            android:textColor="@color/colorAccent"
            android:textStyle="italic"/>
    </RelativeLayout>

How can I achieve expected behavior?

Ébe Isaac
  • 11,563
  • 17
  • 64
  • 97
Choletski
  • 7,074
  • 6
  • 43
  • 64

2 Answers2

0

use spannable

public static SpannableStringBuilder addClickablePart(final Context context, String str) {
    int[] startIndex = {32, 73};
    int[] endIndex = {45, str.length()};
    SpannableStringBuilder ssb = new SpannableStringBuilder(str);
    int count = 0;
    while (count < 2) {
        int idx1 = startIndex[count];
        int idx2 = endIndex[count];
        final String clickString = str.substring(idx1, idx2);
        ssb.setSpan(new ClickableSpan() {

            @Override
            public void onClick(View widget) {
                // Do whatever you want to do
            }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setUnderlineText(false);
                ds.setColor(Color.parseColor("#ffffffff"));
            }
        }, idx1, idx2, 0);
        count++;
    }
    return ssb;
}

adjust the startIndex and endIndex accordingly

Tabish Hussain
  • 852
  • 5
  • 13
0

Use a SpannableString. See the code below.

String part1 = "By logging in, you agree to our ";
String part2 = "terms and conditions";
String fullStr = part1 + part2;
int startIndex = fullStr.indexOf(part2);
int endIndex = fullStr.length();

SpannableString styledString = new SpannableString(fullStr);

// clickable text
ClickableSpan clickableSpan = new ClickableSpan() {

    @Override
    public void onClick(View widget) {
        Toast.makeText(TestActivity.this, "Navigate to terms and conditions page", Toast.LENGTH_SHORT).show();
    }
};
styledString.setSpan(clickableSpan, startIndex, endIndex, 0);

// set color
styledString.setSpan(new ForegroundColorSpan(Color.BLUE), startIndex, endIndex, 0);

TextView out = (TextView) findViewById(R.id.out);
out.setMovementMethod(LinkMovementMethod.getInstance());
out.setText(styledString);

To remove the underline from the ClickableSpan see this SO thread.

Community
  • 1
  • 1
K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33