views:

35

answers:

1

I need a custom dialog to appear on button press. Here is my code:

Button button3 = (Button) findViewById(R.id.Button03); 
    button3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        final Dialog custdialog = new Dialog(this);


        custdialog.setContentView(R.layout.custom_drink);

        custdialog.setTitle("Custom Drink");

        custdialog.setCancelable(false);

 /etc.

However, on this line: final Dialog custdialog = new Dialog(this);

I get an error saying "The constructor Dialog(new View.OnClickListener(){}) is undefined".

I've tried replacing "this" with other things, but with no luck. It's odd that I get an error on this line because I have the exact same thing (except different names and stuff) in my project, beneath a switch (like this:

@Override
 public boolean onOptionsItemSelected(MenuItem item) {
 switch (item.getItemId()) {
    case R.id.settings: 

 final Dialog settingsdialog = new Dialog(this);

    settingsdialog.setContentView(R.layout.custom_dialog);

    settingsdialog.setTitle("Settings");

    settingsdialog.setCancelable(false);

and that one works fine... hmmm.

+1  A: 

First, on your actual question, instead of this, use MyActivity.this, where MyActivity is the name of the Activity class. You are trying to use this from an inner class, but the actual object you need is the outer class.

Also, never use getApplicationContext() for much of anything, particularly anything related to the UI.

CommonsWare
Thanks. I figured it would be an easy fix.
Dan T