Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start an activity from a PreferenceScreen

I have a list of strings (stored in a database, not as a resource) and
I want to allow the user to edit this list. This is easy enough to do with a normal Activity,
but in this application that list should be part of the user preferences.
Basically it's a list of sentences the user wants to have available for use.

Since I want to provide a consistent UI, I want to add this to the preference screen:

<PreferenceScreen xmlns:android="...">
    <!-- Some other categories and preferences here -->
    <PreferenceScreen android:key="PREF_PDT"
                      android:title="Predefined Texts"
                      android:summary="View and edit the list of predefined texts">
    </PreferenceScreen>
    <!-- Some other categories and preferences here -->
<PreferenceScreen>

Now let's say I have a fully working Activity that allows me to edit the texts in the database,
what can I do so that when the user taps the 'PREF_PDT' item the Activity is used?

I take it I'll have to make some modifications to the Activity
or create a custom Preference view of some kind?

Update: So just to be clear, I do not need the 'list' screen to hook into the settings,
I just need to give the users the impression that they are still in the preferences part of the application (without breaking the navigation stack of course). Otherwise they have to go to one place to edit some settings and go to another place to edit the texts. They expect to find everything under 'settings'

Update: I've renamed the question from 'Custom Preference screen to edit a list of items' as it's clear now that what I'm trying to do is launch an activity from a PreferenceScreen. sh404's answer helps but I cannot find the right syntax to refer to the Activity I want to luanch. Perhaps it's monodroid specific. (ActivityNotFoundException)

like image 890
TimothyP Avatar asked Mar 08 '13 05:03

TimothyP


People also ask

What is preference screen in android?

Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. Represents a top-level Preference that is the root of a Preference hierarchy. A PreferenceActivity points to an instance of this class to show the preferences.


1 Answers

write like this:

<Preference
    android:key="mykey"
    android:title="TheTitle"
    android:summary="summary here" >
    <intent android:action="net.hasnath.android.MyActivity"/>
</Preference>

If you need to pass Intent Extras:

<Preference
    android:key="mykey"
    android:title="TheTitle"
    android:summary="summary here" >
    <intent android:action="net.hasnath.android.MyActivity">
        <extra android:name="extra_name" android:value="my_value" />
         <extra android:name="extra_2_name" android:value="my_value_2"/>
    </intent>
</Preference>

Next you'll have to declare the Activity in the AndroidManifest.xml
or you can declare the intent int he preferences screen like this:

<intent android:targetPackage="net.hasnath.android"
        android:targetClass="net.hasnath.android.MyActivity"/>

This way you do not have to make any changes to the AndroidManifest.

like image 64
sha256 Avatar answered Sep 25 '22 23:09

sha256