I am trying to open a dialog when a preference is clicked. The result. It does nothing. Here is the Preference Activity
public class SettingsPrefActivity extends PreferenceActivity {
...
Preference.OnPreferenceChangeListener changeInfoListener = new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference, Object newValue) {
Log.i("TAG","Click in Pref");
dialogCustom(findViewById(android.R.id.content));
return true;
}
};
Preference prefInfo = (Preference) findPreference("pref_info");
prefInfo.setOnPreferenceChangeListener(changeInfoListener);
}
public void dialogCustom(View v) {
Log.i("TAG","Dialog");
final Dialog dialog = new Dialog(getApplicationContext());
dialog.setContentView(R.layout.dialog_info);
dialog.setTitle("INFORMATION");
dialog.setCancelable(true);
Button button = (Button) dialog.findViewById(R.id.buttonOK);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
Here is part of the pref xml
<PreferenceCategory
android:title="ABOUT"
android:key="about_category"
android:layout="@layout/prefcatcustomlayout"
>
<Preference
android:key="pref_version"
android:title="Version"
android:summary="1.0"
/>
<Preference
android:key="pref_info"
android:title="Information"
android:summary=""
/>
</PreferenceCategory>
You have to use prefInfo.setOnPreferenceClickListener
Use this code ....
public class SettingsPrefActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
Preference prefInfo = (Preference) findPreference("pref_info");
prefInfo.setOnPreferenceClickListener(clickListener);
}
Preference.OnPreferenceClickListener clickListener = new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
Log.i("TAG", "Click in Pref");
dialogCustom(findViewById(android.R.id.content));
return true;
}
};
public void dialogCustom(View v) {
Log.i("TAG","Dialog");
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle("INFORMATION");
builder.setNeutralButton("OK",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
dialog.dismiss();
}}).show();
}
}