TimothyP
TimothyP

Reputation: 21745

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)

Upvotes: 13

Views: 8523

Answers (3)

ilidiocn
ilidiocn

Reputation: 380

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>

Upvotes: 0

sha256
sha256

Reputation: 3099

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.

Upvotes: 24

Mehdi Fanai
Mehdi Fanai

Reputation: 4059

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>

Upvotes: -1

Related Questions