How to set multiple click event for the single textview?
Asked Answered
R

5

13

I have a textview as like the following:

txtByRegistering.setText("By Registering you agree to terms and condition and privacy policy");

It is just a big text. So, I used marquee to scroll the text horizontally. that works fine. My Question is, How to invoke the click event while clicking the selected scrolling text .

Say for ex :

  1. when user click the word "Registering" in the above textview, I have to invoke the new Intent.
  2. When the user click on the word "Terms" , I have to invoke another new Intent (An Activity with webview as Terms has URL Link).

As the word "Registering" and "Terms" are Web URLs, I tried something like below :

    String mRegDesc = "By registering you agree to the " + "<a href=\""
            + Constant.URL + "/terms_and_conditions"
            + "\">Terms of Use</a> " + "and " + "<a href=\"" + Constant.URL
            + "/privacy" + "\">Privacy Policy</a> ";

    txtByRegistering.setText(Html.fromHtml(mRegDesc));
    txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance());
    txtByRegistering.setSelected(true);
    txtByRegistering.setTypeface(mTyFaceOverLockReg, Typeface.BOLD);

The above code works fine and it brings me to the browser when i click the word "Terms" But i wish to go to new Activity.

Responsory answered 24/2, 2014 at 16:2 Comment(3)
i think you need ClickableSpan, see my answer on #20989405Purulent
@Shayanpourvatan Can i set multiple ClickableSpan for a Single SpannableString? Would you please more elaborate? And SpannableString supports the API level 8?Responsory
yes you can set multiple Clickabale on one String, and this method added on API level 1 for more info see developer.android.com/reference/android/text/style/… and #9982741Purulent
R
17

Finally,

I found the solution for that,

Here is the solution :

    SpannableString SpanString = new SpannableString(
            "By Registering you agree to the Terms of Use and Privacy Policy");

    ClickableSpan teremsAndCondition = new ClickableSpan() {
        @Override
        public void onClick(View textView) {


            Intent mIntent = new Intent(SignUp.this, CommonWebView.class);
            mIntent.putExtra("isTermsAndCondition", true);
            startActivity(mIntent);

        }
    };

   // Character starting from 32 - 45 is Terms and condition. 
   // Character starting from 49 - 63 is privacy policy. 

    ClickableSpan privacy = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

            Intent mIntent = new Intent(SignUp.this, CommonWebView.class);
            mIntent.putExtra("isPrivacyPolicy", true);
            startActivity(mIntent);

        }
    };

    SpanString.setSpan(teremsAndCondition, 32, 45, 0);
    SpanString.setSpan(privacy, 49, 63, 0);
    SpanString.setSpan(new ForegroundColorSpan(Color.BLUE), 32, 45, 0);
    SpanString.setSpan(new ForegroundColorSpan(Color.BLUE), 49, 63, 0);
    SpanString.setSpan(new UnderlineSpan(), 32, 45, 0);
    SpanString.setSpan(new UnderlineSpan(), 49, 63, 0);

    txtByRegistering.setMovementMethod(LinkMovementMethod.getInstance());
    txtByRegistering.setText(SpanString, BufferType.SPANNABLE);
    txtByRegistering.setSelected(true);

thanks to Shayan pourvatan.

Responsory answered 25/2, 2014 at 7:5 Comment(1)
any idea how to change underline colorMobster
E
2

Let assume here is your complete string

By Signing up, I agree to Terms of Conditions & Privacy Policy

and string you want to make clickable is

Terms of Conditions and Privacy Policy

so, here is my trick.....

ClickableSpan terms = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        new Utils(getActivity()).shortToast("Terms");

    }
};

ClickableSpan privacy = new ClickableSpan() {
    @Override
    public void onClick(View widget) {
        new Utils(getActivity()).shortToast("Privacy");

    }
};

the main function for this

public void setClickableString(String wholeValue, TextView textView, final String[] clickableValue, ClickableSpan[] clickableSpans) {
    SpannableString spannableString = new SpannableString(wholeValue);

    for (int i = 0; i < clickableValue.length; i++) {
        ClickableSpan clickableSpan = clickableSpans[i];
        String link = clickableValue[i];

        int startIndexOfLink = wholeValue.indexOf(link);
        spannableString.setSpan(clickableSpan, startIndexOfLink, startIndexOfLink + link.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    textView.setHighlightColor(
            Color.TRANSPARENT); // prevent TextView change background when highlight
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(spannableString, TextView.BufferType.SPANNABLE);
}

and here is the function calling

setClickableString(getString(R.string.terms_and_policy), tv_terms, new String[]{"Terms of Conditions", "Privacy Policy"}, new ClickableSpan[]{terms, privacy});
Ethaethan answered 3/7, 2018 at 9:58 Comment(1)
Marry me! wink You the bestBerget
X
0

Use this one it works for me two click in single TextView

Step1-: Your text will be in SpannableString

SpannableString ss = new SpannableString("By Registering you agree to terms and condition and privacy policy");

Step2:-add click in ClickableSpan like this

 ClickableSpan Registering = new ClickableSpan() {
        @Override
        public void onClick(View textView) {
            Intent intent=new Intent(this,WebView_Activity.class);
                            startActivity(intent);
        }
        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(true);
        }
    };
    ClickableSpan terms = new ClickableSpan() {
        @Override
        public void onClick(View textView) {

            Intent intent=new Intent(this,WebView_Activity.class);
                            startActivity(intent);
        }
        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(true);
        }
    };

Final step add your click on SpannableString with character starting and ending index like Registering word in start at 3rd position and end at 11 so add click for Registering word

 ss.setSpan(Registering , 3, 11, 0);

same for term after this add your SpannableString on your TextView

  textview.setMovementMethod(LinkMovementMethod.getInstance());
    textview.setText(ss, TextView.BufferType.SPANNABLE);
    textview.setSelected(true);
Xerosis answered 4/12, 2018 at 20:5 Comment(0)
C
0

I would suggest below code for clickable string in TextView it is dynamic. Advantage of this code is if you have same String multiple times you can have click on both Strings. For example if you want to set click and String is Boy is playing cricket. Boy is playing football. Boy is two times both word will be clickable.

    public void setClicksOnString(String completeString, List<String> stringsToClick, TextView textView) {
        SpannableString spannableString = new SpannableString(completeString);
        for (int m = 0; m < stringsToClick.size(); m++) {
            Matcher matcher = Pattern.compile(stringsToClick.get(m)).matcher(spannableString);
            while (matcher.find()) {
                ClickableSpan stringClick = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {
                        //you compare the string and your click logics

                    }
                };
                spannableString.setSpan(stringClick, matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            }
        }
        textView.setHighlightColor(
                Color.TRANSPARENT); // prevent TextView change background when highlight
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setText(spannableString, TextView.BufferType.SPANNABLE);

    }
Capua answered 17/5, 2019 at 6:27 Comment(0)
V
0

In addition, if you want to know in which text the user clicked dynamically, use below

 ClickableSpan listener = new ClickableSpan() {
            @Override
            public void onClick(View textView) {
                TextView tv = (TextView) textView;
                Spanned s = (Spanned) tv.getText();
                int start = s.getSpanStart(this);
                int end = s.getSpanEnd(this);
                String clickedText = s.toString().substring(start,end);
            }
        };
Vector answered 15/1, 2021 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.