<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Saket Narayan on Medium]]></title>
        <description><![CDATA[Stories by Saket Narayan on Medium]]></description>
        <link>https://medium.com/@Saketme?source=rss-f9a20410ae8e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*B-86nsCRTj_JtgnjwRqdIA.jpeg</url>
            <title>Stories by Saket Narayan on Medium</title>
            <link>https://medium.com/@Saketme?source=rss-f9a20410ae8e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:50:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Saketme/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Band-aiding legacy networking libraries with Retrofit and OkHttp]]></title>
            <link>https://medium.com/@Saketme/band-aiding-legacy-networking-libraries-with-retrofit-and-okhttp-dc032be7d283?source=rss-f9a20410ae8e------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc032be7d283</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[retrofit]]></category>
            <category><![CDATA[legacy-code]]></category>
            <dc:creator><![CDATA[Saket Narayan]]></dc:creator>
            <pubDate>Mon, 30 Apr 2018 09:45:38 GMT</pubDate>
            <atom:updated>2018-09-10T16:48:28.605Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*w11zjeezSrmzyV_exvI16Q.jpeg" /></figure><p>When Uncommon was brought in to refactor a big legacy app last year, we found ourselves in a strange situation. Our client was maintaining a common library for handling networking — HTTP, FCM, MQTT — across all apps under their brand. This library (let’s call it SadLibrary) had an API that was badly designed and dated, but not using it was not an option — a dedicated team was working on it and their manager started showing signs of protest.</p><p>Most modern Android apps use <a href="http://square.github.io/retrofit/">Retrofit</a> to access REST-style APIs in an elegant and simple way. We wanted to see if we could make Retrofit work with SadLibrary and decided to give it a shot. Our initial idea was to fork Retrofit as <em>RetroUnfit</em> and plug-in SadLibrary instead of OkHttp. Thankfully, this spectacularly bad idea was shot down in no time. After going through Retrofit’s code, learning how Retrofit delegates calls to OkHttp and, how OkHttp further delegates them to interceptors, we realized that the correct solution was far simpler.</p><h4>OkHttp and The Magical Interceptors</h4><p>For those unaware, OkHttp uses interceptors for not only letting clients hook into calls and modify their request/response bodies, but also for finally running the call on the network. This is done inside the very last interceptor and can be seen in action here: <a href="https://github.com/square/okhttp/blob/878bde8fd6939b508adeb21e2911e390cfe27326/okhttp/src/main/java/okhttp3/RealCall.java#L194">okhttp3.RealCall#194</a>.</p><p>So to make Retrofit work with SadLibrary, all we needed to do was add an interceptor that redirects all network requests from OkHttp and forwards them to SadLibrary. Here’s an example:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o-w3DjZ3ElIEeGAU08wqXw.png" /></figure><p>Using this interceptor is no different from using any other OkHttp interceptor:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yZxL9og3JlVjC7vqXOk3uA.png" /></figure><p>That’s all! Retrofit could now be used for making network calls without dealing with SadLibrary and its sad API!</p><h4><em>Making Retrofit work for you</em></h4><p>Using Retrofit in this manner is very similar to what Nick Butcher does in his beautiful Dribbble client, <a href="https://github.com/nickbutcher/plaid">Plaid</a>. Since Dribbble does not provide a search API, Plaid manually downloads the search page as HTML and parses it using Jsoup. But, instead of leaking this ugliness to the rest of the app, this functionality is exposed through Retrofit as if it were an actual Dribbble API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ccaxwBas4KUBWEMujSbiXw.png" /></figure><p>The response body is parsed inside a Retrofit Converter:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*abFGpCaCr-YczvWRY_1FAA.png" /><figcaption><a href="https://github.com/nickbutcher/plaid/blob/f82480111cdb0a1c9b8b2560670952fc1853eaaa/app/src/main/java/io/plaidapp/data/api/dribbble/DribbbleSearchConverter.java">Source: DribbbleSearchConverter.java</a></figcaption></figure><p>And used like a boss:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D9g6ashpkxUGEKRIh5KmiQ.png" /><figcaption><a href="https://github.com/nickbutcher/plaid/blob/4e338a563bac0eaef3caa7374c93120256871665/app/src/main/java/io/plaidapp/data/BaseDataManager.java#L157">Source: BaseDataManager.java</a></figcaption></figure><p>You can watch Jake Wharton explain this in more detail in his talk, <a href="https://youtu.be/t34AQlblSeE?t=35m50s">Making Retrofit Work For You (GDG Cincinnati)</a>.</p><p>Cover photo by <a href="https://unsplash.com/photos/qHajeosTxBU?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Bryan Colosky</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc032be7d283" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The lesser known world of spans — Droidcon San Francisco]]></title>
            <link>https://medium.com/@Saketme/the-lesser-known-world-of-spans-droidcon-san-francisco-7a1ef4f8be05?source=rss-f9a20410ae8e------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a1ef4f8be05</guid>
            <category><![CDATA[conference]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[spans]]></category>
            <category><![CDATA[droidcon]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Saket Narayan]]></dc:creator>
            <pubDate>Thu, 01 Feb 2018 06:41:46 GMT</pubDate>
            <atom:updated>2018-02-01T06:41:46.516Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MHd053WLcxVaQVWT4aUOQQ.jpeg" /></figure><p>Spans in Android are something that don’t get as much attention as they deserve for how widely they’re used across the platform. I was fortunate to be able to talk about them at <a href="https://sf.droidcon.com/">Droidcon San Francisco</a> this year.</p><blockquote>Do you know what’s common between the blinking cursor, hyperlinks and text highlighting in TextViews? They’re all implemented using spans. Spans are tiny objects that Android uses heavily across the framework.</blockquote><blockquote>If you’ve worked with text styling such as bold or italics, then you’re already familiar with the basic usages of spans. But they are capable of much more than just modifying the visual representation of text like changing indentation, receiving URL clicks, listening to text changes, displaying spelling suggestions, images and much more.</blockquote><blockquote>This talk will do a deep dive on the various spans that Android offers out of the box as well as custom spans and how they can be used for displaying text in any way imaginable.</blockquote><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FG55mJvel7iI%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DG55mJvel7iI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FG55mJvel7iI%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/73d24efc0142327d0c4d98abf950c1f7/href">https://medium.com/media/73d24efc0142327d0c4d98abf950c1f7/href</a></iframe><p>You can also find the slides here: <a href="http://j.mp/lesser-known-world-of-spans-slides">http://j.mp/lesser-known-world-of-spans-slides</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a1ef4f8be05" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deflating the LayoutInflater]]></title>
            <link>https://medium.com/@Saketme/deflating-the-layoutinflater-a16aa0c96415?source=rss-f9a20410ae8e------2</link>
            <guid isPermaLink="false">https://medium.com/p/a16aa0c96415</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Saket Narayan]]></dc:creator>
            <pubDate>Mon, 06 Mar 2017 13:42:52 GMT</pubDate>
            <atom:updated>2017-03-06T13:42:52.917Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2--ddVRTdrjBTp0TFkI-pQ.jpeg" /></figure><p>One of the methods we use a lot in our everyday battles with Android is findViewById(). It lets us find Views from layouts written in XML and returns a reference to their Java objects. And yet we know very little about how this thing works. How do layouts hierarchies written in XML get automagically converted to Java objects and delivered back to us in our Activities?</p><p>To understand this, we need to look at Android as a pure Java framework. For anything written in XML, the framework spends extra effort converting that into Java. Layouts are the best example of this and the class responsible for “inflating” them is LayoutInflater, which is precisely what we’re going to explore in this post.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5gueWT11qd5eADDoKyUizQ.png" /><figcaption>Example usage of LayoutInflater in Fragment#onCreate()</figcaption></figure><h3>Usages</h3><p>The entry point to LayoutInflater is its inflate() method and we use it in a lot of places:</p><ul><li><strong>Fragments</strong>,<strong> </strong>for inflating their layouts.</li><li><strong>Adapter backed Views</strong>,<strong> </strong>for inflating the layout of each item in a RecyclerView, Spinner, etc.</li><li><strong>Activities</strong>: This is not obvious, but every call to setContentView() internally gets routed to LayoutInflater. The source for this can be seen in <a href="https://android.googlesource.com/platform/frameworks/base/+/696cba573e651b0e4f18a4718627c8ccecb3bda0/policy/src/com/android/internal/policy/impl/PhoneWindow.java#216">PhoneWindow</a> (or <a href="https://android.googlesource.com/platform/frameworks/support/+/d25af35/v7/appcompat/src/android/support/v7/app/AppCompatDelegateImplV7.java#242">AppCompatDelegateImplV7</a> when extending AppCompatActivity).</li></ul><h3>Working</h3><p>LayoutInflater begins with the root ViewGroup of a layout and recursively starts inflating its child Views, forming a tree of View objects. For each View found in the layout hierarchy, it instantiates its equivalent Java object in three steps:</p><h4>1. Parsing XML using XmlPullParser</h4><p>The first step involves parsing the View name (e.g., TextView) and its attributes from the XML layout file. This is done using the help of a class known as <a href="https://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html">XmlPullParser</a>, which is an XML parser (just like Moshi for JSON).</p><h4>2. Constructing attributes using AttributeSet</h4><p>After reading a View from XML, the second step involves understanding its plain values read by XmlPullParser and constructing a data-set out of them. This includes:</p><ul><li>Understanding the unit of a value like “24dp”.</li><li>Following the reference to an external layout in &lt;include layout=@layout/include_toolbar&quot; /&gt;.</li><li>Resolving resource identifiers. That is, figuring out if <br>@drawable-v21/button_background.xml should be used over @drawable/button_background.xml depending on the API level.</li><li>Resolving duplicate attributes from different sources. This usually happens when the same property is defined inline on a View as well as inside a style applied on it.</li></ul><h4>3. Instantiating View object using reflection</h4><p>After extracting data in the first two steps, herein lies the most interesting part of LayoutInflater. Using the View’s name and attributes, LayoutInflater <em>attempts</em> to instantiate its Java object in a trial and error manner using <strong>reflection</strong>.</p><p>For those unaware, reflection is like black magic. It lets you examine the type system of a class at runtime, making it possible to access &amp; modify members of a class (fields and methods) that are otherwise private or inaccessible, including invoking the constructors of the class:</p><pre>Class&lt;?&gt; cl = Class.forName(&quot;com.example.SomeClass&quot;);<br>Constructor&lt;?&gt; cons = cl.getConstructor(Param1.class, Param2.class);</pre><p>LayoutInflater uses this same magic for instantiating the View’s object by passing its fully qualified name to Context#getClassLoader()#loadClass().</p><p>For extracting the fully qualified name, LayoutInflater categorizes Views into custom Views and framework Views by using a hack: searching for dots in the name. If dots are found, the View is categorized as a custom View and a framework view otherwise. Beautiful!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Aptw_7Ucs7UCqpiN3yFN3A.png" /><figcaption><a href="https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/view/LayoutInflater.java#783">LayoutInflater.java, line #748</a>.</figcaption></figure><p><strong>a) Framework Views</strong></p><p>For framework Views like TextView, ImageView, etc., which do not require their fully qualified names in XML, LayoutInflater attempts to guess the package name in a trial and error manner:</p><p><a href="https://github.com/android/platform_frameworks_policies_base/blob/master/phone/com/android/internal/policy/impl/PhoneLayoutInflater.java">PhoneLayoutInflater.java</a></p><pre>String[] potentialPackageNames = {<br>    &quot;android.widget.&quot;,<br>    &quot;android.webkit.&quot;,<br>    &quot;android.app.&quot;<br>};</pre><pre>for (String packageName : potentialPackageNames) {<br>    try {<br>        View view = createView(packageName, viewName);<br>        if (view != null) {<br>            return view;<br>        }<br>    } catch (ClassNotFoundException e) {<br>    }<br>}</pre><p>In each iteration, the createView() method attempts to invoke the constructor:</p><pre>TextView textView = (TextView) context<br>        .getClassLoader()<br>        .loadClass(<strong>packageName + &quot;.TextView&quot;</strong>)<br>        .getConstructor()<br>        .newInstance();</pre><p><strong>b) Custom Views</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*boATh0G3uL5f0_X5JJQkhw.png" /></figure><p>On the other hand, for custom Views it is impossible for LayoutInflater to guess the location of their sources. That is exactly why Android requires their fully qualified names in XML layouts, which LayoutInflater can directly use for invoking their constructors:</p><pre>RecyclerView recyclerView = (RecyclerView) getContext()<br>        .getClassLoader()<br>        .loadClass(&quot;<strong>android.support.v7.widget</strong>&quot;)<br>        .getConstructor()<br>        .newInstance();</pre><p>In case the loadClass() call results in a ClassNotFoundException because an invalid View was found in our XML layout, LayoutInflater re-throws it as an “Error inflating class” exception.</p><p>And that’s it.</p><p>With each inflation, LayoutInflater links the instantiated View to its parent ViewGroup and its children Views, essentially creating a tree of our View hierarchy. The View then simply traverses these links every time findViewById() gets called.</p><p>The use of reflection by LayoutInflater makes it an expensive and a slow process, which is one of the reasons we should avoid complex View hierarchies. Not doing so directly affects the startup time for Activities, Fragments or any adapter backed ViewGroup.</p><p>There’s another interesting step involved in inflation of layouts that I decided not to include in this article because of its complexity: <em>Layout Factories</em>. They are classes that can hook into the inflation process for generating their own Views and are used by <a href="https://developer.android.com/topic/libraries/support-library/features.html">AppCompat</a> (for backporting material design to API levels below Lollipop) and <a href="https://github.com/chrisjenx/Calligraphy">Calligraphy</a> (for applying custom fonts without using custom Views). Look at the Calligraphy source for more details on how layout factories are used.</p><p>Looking for an experienced team to solve your mobile problems? We can help you optimise your processes, iterate quicker, and build products that connect better with your users. Write to us at <a href="/service/mailto:hello@uncommon.is">hello@uncommon.is</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a16aa0c96415" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A better way to handle links in TextView]]></title>
            <link>https://medium.com/@Saketme/a-better-way-to-handle-links-in-textview-27bb70b2d31c?source=rss-f9a20410ae8e------2</link>
            <guid isPermaLink="false">https://medium.com/p/27bb70b2d31c</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[linkmovementmethod]]></category>
            <category><![CDATA[uncommon]]></category>
            <category><![CDATA[textview]]></category>
            <dc:creator><![CDATA[Saket Narayan]]></dc:creator>
            <pubDate>Mon, 28 Nov 2016 11:10:18 GMT</pubDate>
            <atom:updated>2018-04-27T13:32:13.337Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tGjHXPW4ChfD2SS79ibilg.jpeg" /></figure><p>There are two ways of “linkifying” URLs in a TextView. First, as an XML attribute:</p><pre>&lt;TextView<br>  ...<br>  android:autoLink=”phone|web” /&gt;</pre><p>and second, programmatically:</p><pre>TextView textView = (TextView) findViewById(R.id.text1);<br>Linkify.addLinks(textView, Linkify.PHONE_NUMBERS|LINKIFY.WEB_URLS);</pre><p>In both the cases, the framework internally registers a LinkMovementMethod on the TextView that handles dispatching a “ACTION_VIEW&quot; Intent when any link is clicked. This is why phone-numbers open in a dialer when clicked, web URLs open in a browser, map URLs open in Google Maps and so on.</p><p>The source can be seen in URLSpan.class (line #63)(<a href="https://github.com/android/platform_frameworks_base/blob/master/core/java/android/text/style/URLSpan.java#L69">link</a>):</p><pre>@Override<br>public void onClick(View widget) {<br>  Uri uri = Uri.parse(getURL());<br>  Context context = widget.getContext();<br>  Intent intent = new Intent(Intent.ACTION_VIEW, uri);<br>  intent.putExtra(Browser.EXTRA_APPLICATION_ID,  <br>  context.getPackageName());</pre><pre>  try {<br>    context.startActivity(intent);<br>  }<br>  ...<br>}</pre><p>The problem with the default LinkMovementMethod is that it’s buggy and non-customizable:</p><p><strong>1. Incorrect touch areas</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2Fing6kGA.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2Fing6kGA.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2Fing6kGA.gif%3Fnoredirect&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="960" height="540" frameborder="0" scrolling="no"><a href="https://medium.com/media/a372474c33f9e508e75a72b5a917796c/href">https://medium.com/media/a372474c33f9e508e75a72b5a917796c/href</a></iframe><p>It incorrectly calculates a URL’s bounds when the URL is present at any horizontal/vertical end and there’s space available in that direction inside the TextView (including its padding). This is like having ghost links in our app and that’s not good.</p><p><strong>2. Unreliable highlighting</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FkPI3P7c.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FkPI3P7c.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FkPI3P7c.gif%3Fnoredirect&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="960" height="540" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac47fa086bddf982438723af764df52b/href">https://medium.com/media/ac47fa086bddf982438723af764df52b/href</a></iframe><p>LinkMovementMethod highlights a URL only the first time it’s clicked and it stops working randomly after that. It also does not correctly track the touch pointer to unhighlight the URL once it’s no longer being touched.</p><p><strong>3. No support for custom URL handling</strong></p><p>We’re also out of luck if we want to show contextual options when a phone-number is clicked instead of simply redirecting to the dialer.</p><h3>BetterLinkMovementMethod</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fi.imgur.com%2FVKrY5QP.mp4&amp;src_secure=1&amp;url=https%3A%2F%2Fi.imgur.com%2FVKrY5QP.gifv&amp;image=https%3A%2F%2Fi.imgur.com%2FVKrY5QP.gif%3Fnoredirect&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;type=video%2Fmp4&amp;schema=imgur" width="960" height="540" frameborder="0" scrolling="no"><a href="https://medium.com/media/2baec91bae42f3665f3016c43ffb96df/href">https://medium.com/media/2baec91bae42f3665f3016c43ffb96df/href</a></iframe><p>Introducing BetterLinkMovementMethod (<a href="https://github.com/saket/Better-Link-Movement-Method">github</a>), a.. uh.. better a version of LinkMovementMethod that solves all our problems. It’s designed to be a drop-in replacement for LinkMovementMethod:</p><pre>TextView textView = (TextView) findViewById(R.id.text1);<br>textView.setMovementMethod(BetterLinkMovementMethod.getInstance());<br>Linkify.addLinks(textView, Linkify.PHONE_NUMBERS);</pre><p>However, the easiest way to get started is by using one of its linkify() methods:</p><pre>BetterLinkMovementMethod.linkify(int linkifyMask, Activity);<br>BetterLinkMovementMethod.linkify(int linkifyMask, ViewGroup);<br>BetterLinkMovementMethod.linkify(int linkifyMask, TextView...);</pre><pre>// Where linkifyMask can be one of Linkify.ALL, Linkify.WEB_URLS,<br>// Linkify.PHONE_NUMBERS, Linkify.MAP_ADDRESSES and <br>// Linkify.EMAIL_ADDRESSES.</pre><p>Update: <a href="https://github.com/Saketme/Better-Link-Movement-Method/releases/tag/v1.1">v1.1</a> introduces a new method for fixing its compatibility with links inserted using Html.fromHtml():</p><pre>BetterLinkMovementMethod.linkifyHtml();</pre><h4>Download</h4><p>Add this to your module’s build.gradle:</p><pre>repositories {<br>  jcenter()<br>}</pre><pre>dependencies {<br>  compile &#39;me.saket:better-link-movement-method:1.0&#39;<br>}</pre><p><strong>Examples</strong></p><p>Registering a BetterLinkMovementMethod on a TextView:</p><pre>BetterLinkMovementMethod.linkify(Linkify.ALL, textView);</pre><p>or on multiple TextViews:</p><pre>BetterLinkMovementMethod.linkify(Linkify.ALL, textView1, textView2, textView3, ...);</pre><p>Adding a click listener:</p><pre>BetterLinkMovementMethod method = BetterLinkMovementMethod.linkify(Linkify.ALL, this);</pre><pre>method.setOnLinkClickListener((textView, url) -&gt; {<br>  // Do something with the URL and return true to indicate that <br>  // this URL was handled. Otherwise, return false to let the <br>  // framework handle the URL.<br>  return true;<br>});</pre><p>Or the less verbose way:</p><pre>BetterLinkMovementMethod<br>  .linkify(Linkify.ALL, this)<br>  .setOnLinkClickListener((textView, url) -&gt; {<br>    // Do something.<br>    return true;<br>});</pre><p>You can also choose to go the shorter route of registering BetterLinkMovementMethod on all TextViews in your Activity’s layout in one go:</p><pre>@Override<br>protected void onCreate(Bundle savedInstanceState) {     <br>  super.onCreate(savedInstanceState);<br>  setContentView(R.layout.activity_main);</pre><pre>  BetterLinkMovementMethod.linkify(Linkify.ALL, this);<br>}</pre><p>When using in a non-Activity context (e.g., Fragments), you can also pass a ViewGroup as the 2nd param:</p><pre>@Nullable<br>@Override<br>public View onCreateView(LayoutInflater inflater, ViewGroup    <br>    container, Bundle savedInstanceState) <br>{ <br>  View view = inflater.inflate(R.layout.your_fragment, ...);<br>  BetterLinkMovementMethod.linkify(Linkify.ALL, ((ViewGroup) view));<br>  return view;<br>}</pre><h4>Source</h4><p>BetterLinkMovementMethod is available on Github. Feel free to <a href="https://github.com/Saketme/Better-Link-Movement-Method/issues">raise issues</a>, <a href="https://github.com/Saketme/Better-Link-Movement-Method/pulls">send contributions</a> or <a href="https://github.com/Saketme/Better-Link-Movement-Method/pulls#fork-destination-box">fork it</a> for your own usage:</p><p><a href="https://github.com/Saketme/Better-Link-Movement-Method">https://github.com/saket/Better-Link-Movement-Method</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27bb70b2d31c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>