Launch new activity from PreferenceActivity

25,992

Solution 1

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.

Solution 2

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>

Solution 3

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>

Solution 4

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);
    }
}

}

Solution 5

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);
Share:
25,992
QuickNick
Author by

QuickNick

I'm QuickNick from Russia. I collect programming languages. @None

Updated on November 21, 2020

Comments

  • QuickNick
    QuickNick over 3 years

    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?

  • faradaj
    faradaj almost 13 years
    Your welcome. :) I guess you should vote up and check the question as answered.
  • QuickNick
    QuickNick almost 13 years
    I'm newbie, where are these options? (I've checked your answer as useful).
  • Josh
    Josh over 12 years
    I like this first option better than the accepted answer as it is cleaner to simply use the "setIntent" on the preference.
  • Andrew T.
    Andrew T. over 11 years
    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.ActivityNam‌​e" />
  • rjh
    rjh almost 10 years
    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.
  • Some Noob Student
    Some Noob Student over 9 years
    Gradle users, if you declared an applicationId in your build.gradle, my solution may help you! stackoverflow.com/a/27053915/499125
  • Some Noob Student
    Some Noob Student over 9 years
    Gradle users, if you declared an applicationId in your build.gradle, my solution may help you! stackoverflow.com/a/27053915/499125
  • Louie Bertoncin
    Louie Bertoncin about 9 years
    You, sir, deserve every liter of love that this planet is capable of producing.
  • Karim O.
    Karim O. almost 8 years
    NOTE! Make sure you also have "import android.preference.Preference.OnPreferenceClickListener;" otherwise trying to set its onClickListener wont work :)