androidstringdynamictextviewclickablespan

How to set multiple click event for the single textview?


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.


Solution

  • 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.