Search code examples
androiddialogonclickandroid-context

Get context inside onClick(DialogInterface v, int buttonId)?


Getting the context inside onClick(View view), the callback for a button's onClickListener(), is easy:

view.getContext()

But I can't figure out how to get the context inside onClick(DialogInterface v, int buttonId), the callback for a dialog's onClickListener

public class MainActivity extends Activity implements android.content.DialogInterface.OnClickListener

Is this possible?


Solution

  • You can reference an outer context when you define your DialogInterface.OnClickListener as an anonymous class. If you're in an activity you can use MyActivity.this as the context.

    Edit - since your Activity is implementing DialogInterface.OnClickListener, you should be able to just use this as the context.