Get event on dialog( Ok ,Cancel) button pressed(Android)
Asked Answered
F

2

1

In my Android application I am displaying the dialogbox which contains edittext. This dialogbox is displayed using PreferenceCategory.My xml file looks like

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="@string/security_setting_edittext_hint" >
        <EditTextPreference
        android:dialogTitle="@string/security_setting_button"
        android:key="set_password_preference"
        android:summary="@string/set_password_summary"
        android:title="@string/security_setting_button"
        android:inputType="number"
        android:icon="@drawable/lock"
         />
    </PreferenceCategory>

</PreferenceScreen>

My Java file looks like

public class Settings extends PreferenceActivity {

    Dialog setPasswordDialog;
    EditText setPassword;
    EditTextPreference editPreference;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setTitle("Settings");
        addPreferencesFromResource(R.xml.preference_authentication);
        editPreference=(EditTextPreference)   findPreference("set_password_preference");

}

There is no issue in displaying the dialog but now i want tho get event when Ok and Cancel button from dialog box is pressed to do something. Please provide me solution.

Flickinger answered 5/3, 2013 at 6:20 Comment(1)
As you want to use the dialogbox displayed using PreferenceCategory, you will need to create a custom EditTextPreference as shown in the answer by @appsroxcom.Sharpshooter
G
3

If I get your question correctly, you want to handle the "Ok" and "Cancel" event and then perform some action based on the response.

// This is using code:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("TITLE HERE");
alert.setMessage("MESSAGE");

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
     //Do something here where "ok" clicked
    }
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    //So sth here when "cancel" clicked.
    }
});
alert.show();
Guanaco answered 5/3, 2013 at 6:33 Comment(1)
Thanks for response. If my understanding about this answer is correct then I don't want to create new dialog box.I want to use the dialogbox displayed using PreferenceCategoryFlickinger
S
3

You will need to create your custom edit text preference as follows.

public class MyEditTextPreference extends EditTextPreference {

    public MyEditTextPreference(Context context) {
        super(context);
    }
    public MyEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public MyEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case DialogInterface.BUTTON_POSITIVE:
        // Put your logic here for Ok button press
        break;

        case DialogInterface.BUTTON_NEGATIVE:
        // Put your logic here for Cancel button press
        break;      
        }
        super.onClick(dialog, which);
    }
}

Then use it in xml file as follows:

<com.package.MyEditTextPreference
android:dialogTitle="@string/security_setting_button"
android:key="set_password_preference"
android:summary="@string/set_password_summary"
android:title="@string/security_setting_button"
android:inputType="number"
android:icon="@drawable/lock"
 />

where com.package should be replaced by the actual package in your project where you create MyEditTextPreference

Sharpshooter answered 5/3, 2013 at 8:50 Comment(1)
this example is really gud but what if we dont wanna dismiss dialog if edittext is null....Hammerfest

© 2022 - 2024 — McMap. All rights reserved.