Android onclick settings
In my preferences.xml, I have a preference item like this:
<Preference android:title="About" /> I want to assign an onClick event, so if a user clicks on it, I can open a new Intent or browser. I tried to do this as with buttons, but this does not work.
Badr
You need to set android:key for the element, then in your code you can do ...
Assuming you are using the following in your XML:
<Preference android:title="About" android:key="myKey"></Preference> Then you can do the following in your code:
Preference myPref = (Preference) findPreference("myKey"); myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { //open browser or intent here return true; } }); launch website:
<PreferenceScreen android:title="website"> <intent android:action="android.intent.action.VIEW" android:data="http://www.example.com" /> </PreferenceScreen> To start a specific operation:
<PreferenceScreen android:title="something"> <intent android:action="android.intent.action.MAIN" android:targetPackage="com.example.foo" android:targetClass="com.example.foo.SomeActivity" /> </PreferenceScreen> you can also use "android: mimetype" to set the mimetype type.
You need to use the onPreferenceTreeClick event.
For example, see http://www.netmite.com/android/mydroid/donut/packages/apps/Phone/src/com/android/phone/CdmaOptions.java
Reply by @jason gilbert
I am on targetSdkVersion 25 and his answer did not work, I had to wrap the intent tag with a Preference tag. Example:
<PreferenceScreen android:title="something"> <Preference title="Title"> <intent android:action="android.intent.action.MAIN" android:targetPackage="com.example.foo" android:targetClass="com.example.foo.SomeActivity" /> </Preference> </PreferenceScreen>