Multiple Clickable links in TextView on Android

后端 未结 8 740
无人共我
无人共我 2020-12-14 07:16

I\'m trying to add multiple links in a textview similar to what Google & Flipboard has done below with their Terms and conditions AND Privacy Po

相关标签:
8条回答
  • 2020-12-14 08:12

    This is working for me :

    in xml :

    <TextView
        android:id="@+id/tv_by_continuing_str"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:textSize="15sp"
        tools:text="Test msg 1 2, 3"
        android:textColor="@color/translucent_less_white3"
        android:textColorLink="@color/white"
        android:gravity="center|bottom"
        android:layout_above="@+id/btn_privacy_continue" />
    

    In strings.xml

    < string name="by_continuing_str2">< ! [ CDATA[By continuing to use this app, you agree to our <a href="https://go.test.com" style="color:gray"/> Privacy Statement </a> and <a href="https://go.test.com" style="color:gray"/>Services Agreement.]]>< / string>
    

    in the activity :

    TextView tv_by_continuing = (TextView) findViewById(R.id.tv_by_continuing);
    tv_by_continuing.setText(Html.fromHtml(getString(R.string.by_continuing_str2)));
    tv_by_continuing.setMovementMethod(LinkMovementMethod.getInstance());
    
    0 讨论(0)
  • 2020-12-14 08:15

    With Textoo, this can be achieved like:

    res/values/strings.xml:

    <resources>
         <string name="str_terms_and_privacy">By signing up you agree to our <a href="terms:">Terms of Service</a> and <a href="privacy:">Privacy Policy.</a></string>
    </resources>
    

    res/layout/myActivity.xml:

    <TextView
        android:id="@+id/view_terms_and_privacy"
        android:text="@string/str_terms_and_privacy"
        />
    

    java/myPackage/MyActivity.java:

    public class MyActivity extends Activity {
        ...
        protected void onCreate(Bundle savedInstanceState) {
            ...
            TextView termsAndPrivacy = Textoo
                .config((TextView) findViewById(R.id.view_terms_and_privacy))
                .addLinksHandler(new LinksHandler() {
                    @Override
                    public boolean onClick(View view, String url) {
                        if ("terms:".equals(url)) {
                            // Handle terms click
                            return true;  // event handled
                        } else if ("privacy:".equals(url)) {
                            // Handle privacy click
                            return true;  // event handled
                        } else {
                            return false;  // event not handled.  continue default processing i.e. launch web browser and display the link
                        }
                    }
                })
                .apply();
            ...
        }
        ...
    }
    

    This approach have the advantages that:

    • Keep text externalized as string resource. Make it easier to localize your app.
    • Can handle the click event directly using LinksHandler and no need to define additional intent filter
    • Simpler and more readable code
    0 讨论(0)
提交回复
热议问题