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

an00b picture an00b · Mar 27, 2011 · Viewed 80.3k times · Source

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?

Answer

Matthew Willis picture Matthew Willis · Mar 27, 2011

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.