Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 871
Rethinavel Avatar asked Feb 24 '14 16:02

Rethinavel


1 Answers

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});
like image 137
Imran Samed Avatar answered Sep 18 '22 11:09

Imran Samed