Last active
November 21, 2019 13:06
-
-
Save NikolaDespotoski/e543b532fb6255c7e39c to your computer and use it in GitHub Desktop.
Open clicked links of TextView in CustomTabs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package samples.despotoski.nikola.com.customtabsmovementmethod; | |
import android.os.Parcel; | |
import android.text.style.URLSpan; | |
import android.view.View; | |
/** | |
* Created by Nikola D. on 12/23/2015. | |
*/ | |
public class CustomTabsURLSpan extends URLSpan { | |
public CustomTabsURLSpan(String url) { | |
super(url); | |
} | |
public CustomTabsURLSpan(Parcel src) { | |
super(src); | |
} | |
@Override | |
public void onClick(View widget) { | |
String url = getURL(); | |
// attempt to open with custom tabs, if that fails, call super.onClick | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package samples.despotoski.nikola.com.customtabsmovementmethod; | |
import android.graphics.Rect; | |
import android.text.Spannable; | |
import android.text.Spanned; | |
import android.text.method.TransformationMethod; | |
import android.text.style.URLSpan; | |
import android.text.util.Linkify; | |
import android.view.View; | |
import android.widget.TextView; | |
/** | |
* Created by Nikola D. on 12/23/2015. | |
*/ | |
public class LinkTransformationMethod implements TransformationMethod { | |
@Override | |
public CharSequence getTransformation(CharSequence source, View view) { | |
if (view instanceof TextView) { | |
TextView textView = (TextView) view; | |
Linkify.addLinks(textView, Linkify.WEB_URLS); | |
if (textView.getText() == null || !(textView.getText() instanceof Spannable)) { | |
return source; | |
} | |
Spannable text = (Spannable) textView.getText(); | |
URLSpan[] spans = text.getSpans(0, textView.length(), URLSpan.class); | |
for (int i = spans.length - 1; i >= 0; i--) { | |
URLSpan oldSpan = spans[i]; | |
int start = text.getSpanStart(oldSpan); | |
int end = text.getSpanEnd(oldSpan); | |
String url = oldSpan.getURL(); | |
text.removeSpan(oldSpan); | |
text.setSpan(new CustomTabsURLSpan(url), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); | |
} | |
return text; | |
} | |
return source; | |
} | |
@Override | |
public void onFocusChanged(View view, CharSequence sourceText, boolean focused, int direction, Rect previouslyFocusedRect) { | |
} | |
} |
That is the best way I found to be working
Thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This does not seem to be working with below TextView and string
<TextView android:id="@+id/text" style="@style/TextAppearance.AppCompat.Small" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/text" />
<string name="text">Test https://google.com</string>
textView.getText() instanceof Spannable returns false
I have tried setting android:autoLink="all" without any luck