Web link in TextView without url display, type "Click here"

1

The goal is to create a dialog with some links to websites.

The following code already does this, but the URLs appear explicit (www ....) and I wish they were 'hidden' behind words like 'click here'.

public void show() {
    PackageInfo versionInfo = getPackageInfo();

    String EULA_PREFIX = "eula_";
    final SpannableString how = new SpannableString(mActivity.getString(R.string.url_how));
    final SpannableString privacy = new SpannableString(mActivity.getString(R.string.url_privacy));
    final SpannableString terms = new SpannableString(mActivity.getString(R.string.url_terms));
    final String eulaKey = EULA_PREFIX + versionInfo.versionCode;
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
    boolean hasBeenShown = prefs.getBoolean(eulaKey, false);
    TextView textView = new TextView(mActivity);

    textView.setText(mActivity.getString(R.string.eula_updates) + "\n\n"
            + mActivity.getString(R.string.str_how) + how + "\n\n"
            + mActivity.getString(R.string.str_privacy) + privacy + "\n\n"
            + mActivity.getString(R.string.str_terms) + terms + "\n\n"
            + mActivity.getString(R.string.str_agree)
    );

    textView.setAutoLinkMask(RESULT_OK);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setPadding(8, 8, 8, 8);

    Linkify.addLinks(textView, Linkify.WEB_URLS);

    String title = mActivity.getString(R.string.app_name) + " v" + versionInfo.versionName;

    AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
            .setTitle(title)
            .setView(textView)
            .setPositiveButton(R.string.button_agree, new Dialog.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialogInterface, int i) {                       
                    SharedPreferences.Editor editor = prefs.edit();
                    editor.putBoolean(eulaKey, true);
                    editor.apply();
                    dialogInterface.dismiss();
                }
            })
            .setNegativeButton(R.string.button_disagree, new Dialog.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {                        
                    mActivity.finish();
                }
            });
    builder.create().show();        
}

To make it clear, if it was HTML, you would get this effect like this:

<a href="www.exemplo.com">clique aqui</a>

So that the link works, but what the user sees and touches is the expression 'click here'

Excerpt from string.xml

        <string name="eula_updates">Updates in this version: Test version. Updates and bugs.</string>
<string name="url_how">https://www.exemplo.com/index.html#header3-2</string>
<string name="url_privacy">https://www.exemplo.com/index.html#header3-6</string>
<string name="url_terms">https://www.exemplo.com.br/index.html#content5-9</string>
<string name="str_how">How to use MOTONOIX </string>
<string name="str_privacy">MOTONOIX privacy policy  </string>
<string name="str_terms">MOTONOIX Terms of Use here </string>
<string name="str_agree">By clicking the button to continue, you declare that you are in compliance with this policy and the terms presented</string>

Can you do this on Android?

    
asked by anonymous 14.03.2017 / 14:49

2 answers

0

With the hint of ramaral, the code looks like this:

 public void show() {
    PackageInfo versionInfo = getPackageInfo();
    String EULA_PREFIX = "eula_";        
    final String eulaKey = EULA_PREFIX + versionInfo.versionCode;
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
    boolean hasBeenShown = prefs.getBoolean(eulaKey, false);
    if (!hasBeenShown) {
        TextView textView = new TextView(mActivity);
        textView.setText(Html.fromHtml(mActivity.getString(R.string.eula_updates) + "\n\n"
                + mActivity.getString(R.string.str_how) + mActivity.getString(R.string.url_how) + "\n\n"
                + mActivity.getString(R.string.str_privacy) + mActivity.getString(R.string.url_privacy) + "\n\n"
                + mActivity.getString(R.string.str_terms) + mActivity.getString(R.string.url_terms) + "\n\n"
                + mActivity.getString(R.string.str_agree))
        );
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        textView.setPadding(8, 8, 8, 8);

        String title = mActivity.getString(R.string.app_name) + " v" + versionInfo.versionName;

        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity)
                .setTitle(title)
                .setView(textView)
                .setPositiveButton(R.string.button_agree, new Dialog.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        // Mark this version as read.
                        SharedPreferences.Editor editor = prefs.edit();
                        editor.putBoolean(eulaKey, true);
                        editor.apply();

                        dialogInterface.dismiss();
                    }
                })
                .setNegativeButton(R.string.button_disagree, new Dialog.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Close the activity as they have declined the EULA
                        mActivity.finish();
                    }
                });
        builder.create().show();
    }
}

And string.xml :

<string name="eula_updates">Updates in this version: Test version. Updates and bugs.</string>
<string name="url_how">&lt;a href="https://www.exemplo.com/index.html#header3-2">touch here&lt;/a>&lt;/p></string>
<string name="url_privacy">&lt;a href="https://www.exemplo.com/index.html#header3-6">touch here&lt;/a>&lt;/p></string>
<string name="url_terms">&lt;a href="https://www.exemplo.com/index.html#content5-9">touch here&lt;/a>&lt;/p></string>
<string name="str_how">&lt;p>"How to use MOTONOIX "</string>
<string name="str_privacy">&lt;p>"MOTONOIX privacy policy "</string>
<string name="str_terms">&lt;p>"MOTONOIX Terms of Use "</string>
<string name="str_agree">&lt;p>"By clicking the button to continue, you declare that you are in compliance with this policy and the terms presented.&lt;/p></string>
    
14.03.2017 / 19:08
5

One of the possible ways is to declare a resource string like this:

<string name="link_example">&lt;a href="http://www.exemplo.com">Clique aqui&lt;/a></string>

and use it like this in java

textView.setText(Html.fromHtml(getString(R.string.link_example)));
textView.setMovementMethod(LinkMovementMethod.getInstance());

When building HTML you should:

  • Include http: // in the link.
  • use &lt; instead of <
14.03.2017 / 15:05