tags:

views:

407

answers:

5

I have a DialogPreference and I want to avoid the user from closing it when pressing "OK", "Cancel", etc.

How should I do that?

EDIT:

I tried to reach the OK button to disable when the dialog is created. But I couldn't make it :(

+1  A: 

You could try opening it again.

Why would you want to prevent users to close the dialog? Users should be able to have 'full' control of their device.

MrSnowflake
force the user to select an option.
Macarse
Yeah, I get that, but then the user isn't in control anymore.
MrSnowflake
For example if a user brings up a dialog to enter their age, and they enter "xyz" for their age, or they enter nothing for their age, they shouldn't be able to click "OK". However, I can't think of an instance when they shouldn't be able to hit cancel. What if they accidently brought up the dialog by clicking a wrong button?
Jay Askren
@Jay: That's true. And that's how it should work. I just want to disable the "OK" button.
Macarse
Hmm I assume you can when you implement your own AlertDialog class.
MrSnowflake
+2  A: 

A tweak could be to create a custom dialog where you define your own buttons (OK and Close).

public class YourClass implements OnClickListener {
    private Button DialogButton;
    private Dialog dialog;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.MainLayout);

        /* Your code... */

        DialogButton = (Button) findViewById(R.id.DialogButtonId);
        DialogButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.DialogButtonId:
            LayoutInflater inflater = LayoutInflater.from(YourClass.this);
            final View inflay = inflater.inflate(R.layout.DialogLayout, (ViewGroup) findViewById(R.id.RootIdOfDialogLayout));

            TextView YourTextView = (TextView) inflay.findViewById(R.id.TextViewId);

            Button cancel = (Button) inflay.findViewById(R.id.CancelButtonId);      
            cancel.setOnClickListener(YourClass.this);

            Button ok = (Button) inflay.findViewById(R.id.OkButtonId);      
            ok.setOnClickListener(YourClass.this);

            dialog = new Dialog(YourClass.this);
            dialog.setContentView(inflay);
            dialog.setTitle(getString(R.string.TitleStringId));
            dialog.show();
            break;
        case R.id.CancelButtonId:
            /* Checking if the user selected an option if true call dialog.dismiss() */
            break;
        case R.id.OkButtonId:
            /* Here handle your preferences (e.g. putString(String key, String value)) */
            /* Checking if the user selected an option if true call dialog.dismiss() */
            break;
        }
    }
}

Check out http://developer.android.com/reference/android/content/SharedPreferences.Editor.html in order to handle your preference in onClick. I didn't test this code just wrote it to show you how you could solve it!

The dialog stays open until you call dialog.dismiss();. In that case you'll have to create your drop-down-menu, polls or what ever you want to display in your layout file. After pressing ok or cancel you should check if the user made a choice, and parse that choice into your preferences. (check link above)

Rgds Layne

Layne
Thanks but I don't want to do that. I am willing to find a solution extending from DialogPreference.
Macarse
A: 

How about overriding the onDismiss() method and implementing a canExit() method with the validations you want to occcur? E.g. :

public class MyDialogPref extends DialogPreference {

  @override public void onDismiss(DialogInterface dialog) {
    if (canExit()) {
      super.onDismiss(dialog);
    }
  }
  ...
}
JRL
I tried that. But if onDismiss() is called, it doesn't matter if you call super.onDismiss() or not, the dialog is closed.
Macarse
+1  A: 

You can see the source code of DialogPreferences here:

http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=core/java/android/preference/DialogPreference.java;h=cc48aeb70844986fa47f923d4b921425309120a0;hb=19563cc278446c2df7a2df6d5f7c89f43228c437

And then, copy most of it to your code, modifying the code as needed.

yuku
A: 

A good UI should have a default selection/option already selected (the previously user-entered options or a program default).

Presenting a dialog asking for a change in options without any indication of what you already have is bad UI design.

This way if the user clicks Cancel, nothing changes and they saw what the option selected was. If they make no change and click OK then nothing really changes either.

Software is supposed to make doing specific tasks easier, not force the user to process the apps logic themselves.

ExitToShell