Web link in Textview without showing url, type "Click here"

Asked

Viewed 108 times

1

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

The following code already does this, but the URL appear explicitly (www....) and I would like it to be '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 were HTML, you would get this effect like this:

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

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

Stretch of 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>

You can do this on Android?

2 answers

5

One of the possible ways is to declare a Resource string in this way:

<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 must:

  • include http:// in link.
  • use &lt; instead of <
  • was your tip that gave me the answer, but since there are several links and texts, I decided to add a response with modified code, including the strings.xml which I also added to the question to compare the versions. Thank you again. You are great!

0


With the ramaral tip, the code went 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 the 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.