37

Good day, friends. I have a PreferenceActivity, it is filled from XML file. When we press one item, we should launch new activity. How to do it? What should I write in XML-file or in the Java-class?

5 Answers 5

77

Given you are using xml preferences you can add code right into the xml:

<Preference
    android:title="Some Title"
    android:summary="Some Description">

    <intent 
        android:action="android.intent.action.VIEW"
        android:targetPackage="com.package.name"
        android:targetClass="com.package.name.ActivityName"
    />

</Preference>
3
  • 24
    For those who are getting ActivityNotFoundException s here after trying to launch their activity in preferences, you might be using subpackage names improperly, like I was doing. If that's the case, use only subpackages in the targetClass section. <intent android:action="android.intent.action.VIEW" android:targetPackage="com.package.name" android:targetClass="com.package.name.subpackage.ActivityName" />
    – Andrew T.
    Feb 6, 2013 at 18:58
  • To maybe save some people pain: you can define category and extra data in XML tags inside <intent>, but you CANNOT set intent flags using XML. If you want to set flags, you need to register a OnPreferenceClickListener and launch the Intent manually.
    – rjh
    Jul 23, 2014 at 12:29
  • 2
    Gradle users, if you declared an applicationId in your build.gradle, my solution may help you! stackoverflow.com/a/27053915/499125 Nov 21, 2014 at 4:10
62

After you add preferences using

addPreferencesFromResource(R.xml.preferences);

find your preference that you want to set onClick using

findPreference("foo_bar_pref");

and define it by casting like

Preference fooBarPref = (Preference) findPreference("foo_bar_pref");

Then you can easily set its onClick using

fooBarPref.setOnPreferenceClickListener (new OnPreferenceClickListener()){...}

You can start your new Activity (using an Intent) inside that listener.

4
  • Your welcome. :) I guess you should vote up and check the question as answered.
    – faradaj
    Aug 17, 2011 at 5:12
  • I'm newbie, where are these options? (I've checked your answer as useful).
    – QuickNick
    Aug 17, 2011 at 9:07
  • Gradle users, if you declared an applicationId in your build.gradle, my solution may help you! stackoverflow.com/a/27053915/499125 Nov 21, 2014 at 4:07
  • NOTE! Make sure you also have "import android.preference.Preference.OnPreferenceClickListener;" otherwise trying to set its onClickListener wont work :)
    – Karim O.
    Sep 26, 2016 at 23:51
18

Gradle Builders, Look Over Here!

If you are using gradle over ant as your build tool, and you declared an applicationId inside android.

[build.gradle]:

android {
    defaultConfig {
        ...
        applicationId "com.overriding.package.name"
    }
    ...
}

This will overwrite whatever value you declared in AndroidManifest.xml's android:package as your app's unique identifier!

[AndroidManifest.xml]

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.package">
    <activity android:name=".settings.MyActivity"/>
</manifest>

The <intent> would have to take both package names into account!

<Preference
    android:title="Some Title"
    android:summary="Some Description">

    <intent 
        android:targetPackage="com.overriding.package.name"
        android:targetClass="com.my.package.settings.MyActivity/>
</Preference>
1
  • 1
    You, sir, deserve every liter of love that this planet is capable of producing. Jun 19, 2015 at 16:42
12

This is nice tutorial for add preferences dynamically...later you have to customized its own way.

In XMl :

<Preference  android:key="key" android:title="See Android Market"></Preference>

In Java class:

Preferences preferences=findPreference("key");
               preferences.setIntent(new Intent(Intent.ACTION_VIEW,Uri.parse("https://market.android.com/")));

OR

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.DialogPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.widget.LinearLayout;
import android.widget.ListView;

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /* Some initializations */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    ListView listView = new ListView(this);
    listView.setId(android.R.id.list);
    listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 1));
    layout.addView(listView);

    this.setContentView(layout);
    /* Preferences time! (we build the preferences) */
    Preference version = getPreference("My School Manager", "Version 2.0",null);
    Preference author = getPreference("Author", "Balu", null);
    Preference marketLink = getPreference("Android market","View all my apps :)",new Intent(Intent.ACTION_VIEW, Uri.parse("http://market.android.com/")));

    CheckBoxPreference check = new CheckBoxPreference(this);
    check.setTitle("Checkbox");
    check.setSummary("Example of checkbox");
    DialogPreference license = new MyDialogPreference(this, "License","This is the license for...bla bla");

    /* Now we add the preferences to the preference screen */
    PreferenceScreen preferenceScreen = this.getPreferenceManager()
            .createPreferenceScreen(this);
    addPreferenceCategory(preferenceScreen, "Preferences Tutorial",version, author, marketLink, check, license);
    this.setPreferenceScreen(preferenceScreen);
}
private boolean addPreferenceCategory(PreferenceScreen preferenceScreen,
        String titleCategory, Preference... preferences) {
    boolean addPreference = false;
    for (Preference preference : preferences) {
        if (preference != null)
            addPreference = true;
    }
    if (addPreference) {
        PreferenceCategory preferenceCategory = new PreferenceCategory(this);
        preferenceCategory.setTitle(titleCategory);
        preferenceScreen.addPreference(preferenceCategory);
        for (Preference preference : preferences) {
            if (preference != null)
                preferenceCategory.addPreference(preference);
        }
        return true;
    } else
        return false;
}
private Preference getPreference(String title, String summary, Intent intent) {
    Preference pref = new Preference(this);
    pref.setTitle(title);
    pref.setSummary(summary);
    if (intent != null)
        pref.setIntent(intent);
    return pref;
}

public class MyDialogPreference extends DialogPreference {
    public MyDialogPreference(Context context, String title, String text) {
        super(context, null);
        this.setTitle(title);
        this.setDialogMessage(text);
    }
}

}

1
  • 1
    I like this first option better than the accepted answer as it is cleaner to simply use the "setIntent" on the preference.
    – Josh
    Mar 27, 2012 at 17:44
1

You have to register an onClickListener to the view you want to launch the activity. Then, inside this method, you just need to invoke the activity with an intent. Something like this:

Intent intent = new Intent(this, ActivityToLaunch.class);

// Start boardgame
startActivity(intent);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.