0

I am using a text view in my aap, which is having plane text as well as a hyperlink. Now when I click on hyperlink then link open with default browser. But in actual I dont want to open default browser. Actually I want to register OnClickListener on hyperlink and want to perform other.

I searched on internet and I got this solution...

Control onclicklistener in autolink enabled textview

But this is not helpful for me.

Anyone can tell me that how I can perform this.

Thanks in advance

Community
  • 1
  • 1
Sanni Raj
  • 127
  • 1
  • 9

5 Answers5

1

you can use a Spannable object

final Spannable span = new SpannableString(text);
span.setSpan(new ClickableSpan() {
      @Override
      public void onClick(View v) {

      }
}, 0, text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

where text is your hyperlink

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

Remove android:autoLink="web" if this property setted into XML.

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);

when you want to open in browser use this code

textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));

if you want to perform some operation register onclick listener for textview and perform.

textView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                }
            });
Gopi Krishna
  • 172
  • 1
  • 8
  • Actually my text is combination of plain text and hyperlink like this - China Cybersecurity Agreement Will Leave U.S. Companies Vulnerable $AAPL http://t.co/eEFtINFONE http://t.co/Qul0ugQrIN. But you are giving solution for whole textview – Sanni Raj Sep 28 '15 at 12:51
1

Try doing this add in your main.xml

<TextView
       android:id="@+id/yourTVID"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:onClick="performSomeAction" />

in your SomeActivity.java

public void performSomeAction( View v){ 
//Perform your action
}
1binary0
  • 93
  • 11
1

Try this, it should solve your problem. This method will return a Spannable String which have part of it clickable.

Before calling the below method you should Create CharSequence from the String then convert it to Spannable

CharSequence charSequencce = testView.getText();
    Spannable spannable = (Spannable) charSequencce;

public SpannableStringBuilder addClickToPartsOfString(Spannable charSequence, String[] stringsToAddClick, final OnHyperLinkClickListener onClickListener) {
    SpannableStringBuilder  ssb = new SpannableStringBuilder(charSequence);

    for(final String s : stringsToAddClick) {
        int index1 = charSequence.toString().indexOf(s);
        int index2 = (s.length() + index1);
        ssb.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                onClickListener.onClick(s);
            }
        }, index1, index2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return ssb;
}
Suru
  • 697
  • 8
  • 20
  • Create an interface with OnHyperLinkClickListener name and write abstract method onClick(String s); public interface OnHyperLinkClickListener { public void onClick(String subString); } – Suru Sep 28 '15 at 12:52
  • In String[] stringsToAddClick parameter you have to pass the parts of strings which you want to hyperlink. – Suru Sep 28 '15 at 12:58
  • Getting exception at ssb.setSpan(). java.lang.IndexOutOfBoundsException: setSpan (-1 ... 47) starts before 0 – Sanni Raj Sep 28 '15 at 13:10
  • Thank you so much for quick reply. I achieved this using SpannableStringBuilder :) – Sanni Raj Sep 30 '15 at 08:34
0

I've just made a library aiming to simplify this. See Textoo. You can achieve the same with code like:

    TextView locNotFound = Textoo
        .config((TextView) findViewById(R.id.view_location_disabled))
        .addLinksHandler(new LinksHandler() {
            @Override
            public boolean onClick(View view, String url) {
                if ("internal://settings/location".equals(url)) {
                    Intent locSettings = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(locSettings);
                    return true;
                } else {
                    return false;
                }
            }
        })
        .apply();

Internally the library converts existing links in your textview / string resources (android system parse html links in string resources into Span for you already) into custom ClickableSpan and capture clicks into calls to your handlers.

This relieve you from having to calculate and hard coding the position of clickable spans to add. Thus make it easier to externalize your text into string resources and better for localization.

PH88
  • 1,796
  • 12
  • 12