13

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)

TimothyP
  • 21,178
  • 26
  • 94
  • 142
  • You might want to look at PreferenceFragments http://developer.android.com/reference/android/preference/PreferenceFragment.html – Premsuraj Mar 08 '13 at 05:57
  • I'm not entirely sure how that helps me – TimothyP Mar 08 '13 at 06:01
  • Possible duplicate of [Launch new activity from PreferenceActivity](http://stackoverflow.com/questions/7041292/launch-new-activity-from-preferenceactivity) – Flow Aug 20 '16 at 12:17

3 Answers3

24

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.

Flow
  • 23,572
  • 15
  • 99
  • 156
sha256
  • 3,029
  • 1
  • 27
  • 32
  • This is obviously what I'm looking for, but I can't get the 'action' part right... tells me it cannot find the Activity, might be Monodroid specific syntax, looking in to it now – TimothyP Mar 08 '13 at 06:50
  • you need to declare the activity in AndroidManifest.xml – sha256 Mar 08 '13 at 10:47
  • Ah ok... will take a look at that, let me get back to you on it – TimothyP Mar 08 '13 at 13:24
  • You don't have to declare `Activity` in manifest if using MonoDroid, but be careful about the name in `android:action`, it should be lower-cased package name and then your whatever-casing class name. – Aaron He Mar 08 '13 at 18:08
  • @AaronHe I tried about every variation I could think of but couldn't get it working. In the end I used targetPackage and TargetClass as seen in the updated answer – TimothyP Mar 09 '13 at 01:50
  • Works awesome!...any way to get a return value from the custom activity and set it in the `Preference` summary? – James Poulose Jul 09 '17 at 23:24
0

Set the activity this way in the manifest file

<activity android:name=".MainActivity">
     <intent-filter>
            <action android:name=".MainActivity" />
       <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

and in the preference file

<Preference
     android:key="preferred_key"
     android:title="@string/preferred_title"
     android:summary="@string/preferred_key_summary">
         <intent android:action=".MainActivity"/
</Preference>
ilidiocn
  • 323
  • 2
  • 5
-1

I had the same issue but none of the solutions i searched on stackoverflow solved my activitynotfound Exception.

Here is the working solution i found from here:

    <PreferenceScreen  
                android:title="@string/title_intent_preference"  
                android:summary="@string/summary_intent_preference">  

            <intent android:action="your.action.string"/>  

 </PreferenceScreen>  

set an intent filter in your activity inside manifest.xml

    <activity ...>  
            <intent-filter>  
                <action android:name="your.action.string"/>  
                <category android:name="android.intent.category.DEFAULT" />  
            </intent-filter>  
</activity>
Mehdi Fanai
  • 4,021
  • 13
  • 50
  • 75