0

What I need is to show message but do nothing. I've tried with "break" and "return true/false". Any suggestion? And my code is:

builder.setPositiveButton("OK", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
    if(chosenCat.size() < 2){
        Toast.makeText(getApplicationContext(), "Min 2 categories to compare.", Toast.LENGTH_SHORT).show();
        //...needs to abort OK button like nothing pressed
    } else {
        //...do some stuff
}
}
});
Arnes
  • 403
  • 1
  • 5
  • 20

3 Answers3

0

To keep a dialog open after one of the buttons has been pressed please see this post.

You can add an onShowListener to the AlertDialog where you can then override the onClickListener of the button.

final AlertDialog d = new AlertDialog.Builder(context)
        .setView(v)
        .setTitle(R.string.my_title)
        .setPositiveButton(android.R.string.ok,
                new Dialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface d, int which) {
                        //Do nothing here. We override the onclick
                    }
                })
        .setNegativeButton(android.R.string.cancel, null)
        .create();

d.setOnShowListener(new DialogInterface.OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {

        Button b = d.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // TODO Do something

                //Dismiss once everything is OK.
                d.dismiss();
            }
        });
    }
});

Code reproduced from above link for reference.

Community
  • 1
  • 1
Phil Applegate
  • 567
  • 2
  • 9
0

try to use dialog.getButton(Dialog.BUTTON1).setEnabled(false);

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setPositiveButton("OK", new OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) {
        if(chosenCat.size() < 2){
        Toast.makeText(getApplicationContext(),"Min 2 categories to compare.", Toast.LENGTH_SHORT).show();
        }
}
});

After this create create dialog like the following

AlertDialog dialog = builder.create();
dialog.show();

After called show() check your condition

if(chosenCat.size() < 2){
    dialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(false);
}

I hope this will help you.

Gunaseelan
  • 14,415
  • 11
  • 80
  • 128
-1

What is your exact requirement. From the condition:

    if(chosenCat.size() < 2){
    Toast.makeText(getApplicationContext(), "Min 2 categories to compare.", Toast.LENGTH_SHORT).show();

from the above condition I understand you are trying to cancel the OK button only in case there are less selected options than the minimum.

Rather than cancel, why don't you set "isEnabled()" param of the OK button to false. and if and only if there are minimum selections the OK button is visible. Say:

if(chosenCat.size()<2){
   button1.isEnabled()="false";
Toast.makeText(getApplicationContext(), "Select minimum 2 options", Toast.LENGTH_SHORT).show();
else {
builder.setPositiveButton("OK", new OnClickListener() 
{
       //DO SOMETHING
 }
 });
Amrit
  • 2,295
  • 4
  • 25
  • 42