android - Preference with Confirm Dialog -
i having hard time making preference dialog, none of methods suggested in site works me. here have first. in mainactivity have this
@override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_stage_1, menu); return true; }
this opens menu, , when choose settings, in next screen there 1 preference, created preferences.xml
<preferencescreen xmlns:android="http://schemas.android.com/apk/res/android" > <preference android:summary="clears data" android:title="reset data" > <intent android:targetclass="com.example.myapp.settings.dialogactivity" android:targetpackage="com.example.myapp" > </intent> </preference>
i want when click option, dialog appear yes , no button, ca define somewhere buttons programatically. how can it?
you can extend dialogpreference. override ondialogclosed
method, called when dialog
closes. argument method indicates whether user selected positive
or negative
button.
customdialogpreference.java :
public class customdialogpreference extends dialogpreference { public customdialogpreference(context context, attributeset attrs) { super(context, attrs); // set layout here setdialoglayoutresource(r.layout.custom_dialog); setpositivebuttontext(android.r.string.ok); setnegativebuttontext(android.r.string.cancel); setdialogicon(null); } @override protected void ondialogclosed(boolean positiveresult) { // when user selects "ok", persist new value if (positiveresult) { // user selected ok } else { // user selected cancel } } }
preferences.xml
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <com.example.test1.customdialogpreference android:key="pref_dialog" android:title="dialog"/> </preferencescreen>
you can refer official settings documentation.
Comments
Post a Comment