Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch new activity from PreferenceActivity

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?

like image 521
QuickNick Avatar asked Aug 12 '11 14:08

QuickNick


3 Answers

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>
like image 143
ggenglish Avatar answered Oct 17 '22 20:10

ggenglish


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.

like image 45
faradaj Avatar answered Oct 17 '22 21:10

faradaj


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>
like image 27
Some Noob Student Avatar answered Oct 17 '22 20:10

Some Noob Student