I want to assign an ...">

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.

+86
java android android-preferences
Mar 16 '11 at 19:27
source share
4 answers

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; } }); 
+187
Mar 16 '11 at 19:31
source share

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.

+47
Aug 31 2018-11-21T00:
source share
+11
Mar 16 '11 at 19:31
source share

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> 
+2
May 17 '17 at 11:17
source share



All Articles