15

I would like to add three hyperlinks a Preference screen, which makes use of the PreferenceActivity if possible. Can I do this, if so, could someone provide support?

Many thanks,

user2511675
  • 159
  • 2
  • 9
  • Could you be more specific about what you mean by "hyperlinks" in the context of a `PreferenceScreen`? Where do you expect these "hyperlinks" to appear? What do you expect them to look like? What do you expect to happen when the user clicks upon one? – CommonsWare Jul 13 '13 at 22:16
  • I want the user, when they click to be taken to a webpage on the developers website. I expect the hyperlinks to appear, in the Preference screen, typically in the list (much like Twitters settings page) and I want them to do look fairly basic. – user2511675 Jul 13 '13 at 22:23

2 Answers2

46

You don't want "hyperlinks", then. You want entries in the PreferenceScreen that, when tapped, launch some activity, such as to bring up a Web page on your desired URL.

That is covered by the <intent> element:

<Preference android:title="@string/prefs_web_page" >
    <intent android:action="android.intent.action.VIEW"
            android:data="http://www.example.com" />
</Preference>

Include those in your preference XML that you use to populate your PreferenceScreen, and when the user taps on the preference entry, your requested activity will start.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Okay, I will try that soon. How will the text be displayed and how to I edit the text etc? – user2511675 Jul 13 '13 at 22:35
  • @user2511675: The text for the entry is the `android:title` attribute, shown in the sample as a reference to a string resource. – CommonsWare Jul 13 '13 at 22:36
  • Okay, but how can I style that text? – user2511675 Jul 13 '13 at 22:41
  • @user2511675: Ideally, you don't. You want it to match the way the rest of your preferences look in the screen. And if you don't have any other preferences, there is no reason at all to use a preference screen for this in the first place. That being said, your string resources can use ``, ``, and `` HTML-style tags, and you can always call `setTitle()` on the `Preference` from Java code and supply your own `CharSequence`, such as a `SpannableString` to which you have applied various `CharacterStyles`. Overall, your theme drives the look of the preference screen itself. – CommonsWare Jul 13 '13 at 22:44
  • Okay. Thanks for explaining it. This page is used to display Help, Terms or Service and Privacy Policy, can you think of a better way of doing it? I like the way Twitter does it with it opening up within the app. How would I go about doing that? – user2511675 Jul 13 '13 at 22:55
  • @user2511675: "This page is used to display Help, Terms or Service and Privacy Policy, can you think of a better way of doing it?" -- have three overflow items as part of your action bar, one for each. – CommonsWare Jul 13 '13 at 23:08
  • Okay, but how are they actually displayed? I.e where is the text shown? – user2511675 Jul 13 '13 at 23:20
  • @user2511675: If by "the text", you mean the actual privacy policy, etc., I am assuming those are up on your Web site, and therefore your action bar items would use `startActivity()` to open a Web browser. Or, if you want to bundle the HTML into the app, you can use a `WebView`. – CommonsWare Jul 13 '13 at 23:24
  • If I was to use a startAcitivity() how would I link them to the webpage? I.e what code goes under the startActivity(). Thanks for being so patient. – user2511675 Jul 13 '13 at 23:30
  • 1
    @user2511675: `startActivity(new Intent(Intent.ACTION_VIEW, "http://you.really.can.find/this/stuff/out/by-searching"));` – CommonsWare Jul 13 '13 at 23:31
  • Ta. I think I will use that webpage link somewhere mate ;) – user2511675 Jul 13 '13 at 23:39
2

Java version is something like this:

PreferenceScreen link = man.createPreferenceScreen(this);
this.setIntent(new Intent().setAction(Intent.ACTION_VIEW).setData(
           Uri.parse("https://rogerkeays.com")));
link.setTitle(R.string.link_title);
link.setSummary(R.string.link_summary);

// add to containing group
// group.addPreference(link);
Roger Keays
  • 3,117
  • 1
  • 31
  • 23