TryTryAgain
TryTryAgain

Reputation: 7820

Adding a Cancel Button to a CustomAlertDialog

I am just a themer, not a programmer, any help/guidance is appreciated. I am trying to add a Cancel button to this code:

public class gobuuf extends Activity {
/** Called when the activity is first created. */

class CustomAlertDialog extends AlertDialog {
    public CustomAlertDialog(Context context) {
        super(context);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        boolean ret = super.onKeyDown(keyCode, event);
        finish();
        return ret;
    }

    public void setCancel(int buttonNegative, String string, Object object) {
        // TODO Auto-generated method stub

    }
}

private CustomAlertDialog mDialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (isExistSkin("com.gau.go.launcherex")) {
        startGOLauncher("com.gau.go.launcherex");
        finish();
        return;
    }

    mDialog = new CustomAlertDialog(this);
    mDialog.setTitle(R.string.dialogtitle);
    mDialog.setMessage(getResources().getString(R.string.dialogcontent));
    mDialog.setCancel(DialogInterface.BUTTON_NEGATIVE, getResources().getString(R.string.dialognothanks), null);
    mDialog.setButton(DialogInterface.BUTTON_POSITIVE, getResources().getString(R.string.dialogok),
            new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            String marketuriString = "market://search?q=pname:com.gau.go.launcherex";
            Intent EMarketintent = new Intent(Intent.ACTION_VIEW, Uri.parse(marketuriString));
            EMarketintent.setPackage("com.android.vending");
            EMarketintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//              Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(marketuriString));     
            try {
                startActivity(EMarketintent);
            } catch (ActivityNotFoundException e) {
                String link = "http://61.145.124.93/soft/3GHeart/com.gau.go.launcherex.apk";
                Uri browserUri = Uri.parse(link);
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, browserUri);
                browserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                try {
                    startActivity(browserIntent);
                } catch (ActivityNotFoundException e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }
            } catch (Exception e3) {
                // TODO: handle exception
                e3.printStackTrace();
            }
            finish();

        }
    });
    mDialog.show();
}


@Override
protected void onDestroy() {
    super.onDestroy();
    android.os.Process.killProcess(android.os.Process.myPid());
}

private boolean isExistSkin(String packageName) {
    try {
            createPackageContext(packageName,
                    Context.CONTEXT_IGNORE_SECURITY);
        } catch (NameNotFoundException e) {
            return false;
        }
    return true;
}

private void startGOLauncher(String packageName){
    PackageManager packageMgr = this.getPackageManager();
    Intent launchIntent = packageMgr.getLaunchIntentForPackage(packageName);
    if (null != launchIntent){
        try
        {
            this.startActivity(launchIntent);
        }
        catch(ActivityNotFoundException e)
        {

        }
    }
}
}

I have added the corresponding string, but don't know what else to do: add an onclicklistener, I know I need some kind of action coded somewhere finish(); or .cancel something. Thanks for any help, again.

I should say, I've been playing around with this bit you see:

mDialog.setCancel(DialogInterface.BUTTON_NEGATIVE,getResources().getString(R.string.dialognothanks), null);

Upvotes: 0

Views: 450

Answers (3)

TryTryAgain
TryTryAgain

Reputation: 7820

I figured out how to start an activity by following the answer on this page: Android: How to start an Activity from an alert dialog

So my code now looks like:

mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getResources().getString(R.string.dialognothanks),
            new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent i = new Intent(getBaseContext(), AboutActivityOverview.class);    
                        startActivity(i);
                                }
                        });

Upvotes: 0

TryTryAgain
TryTryAgain

Reputation: 7820

It's now working and performing how I wanted it to by doing this:

1) I removed:

public void setCancel(int buttonNegative, String string, Object object) {
    // TODO Auto-generated method stub
}

and

mDialog.setCancel(DialogInterface.BUTTON_NEGATIVE, getResources().getString(R.string.dialognothanks), null);

2) and added:

mDialog.setButton(DialogInterface.BUTTON_NEGATIVE, getResources().getString(R.string.dialognothanks),
            new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                                mDialog.cancel();
                                        finish();
                                }
                        });

just before the

 mDialog.show();

Trial and Error FTW. Maybe I should learn Java and Android from the ground up to save me the time later.

Now I have to learn how to make the Cancel button not only finish(); but also start another activity.

Upvotes: 0

Kgrover
Kgrover

Reputation: 2116

I am not sure if this is exactly what you are looking for, but you can do something like this... this one just closes the dialog.

.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }

For a very good tutorial on creating dialogs, I would recommend using : http://developer.android.com/guide/topics/ui/dialogs.html. This should be very helpful.

Upvotes: 3

Related Questions