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?
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>
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With