Tuesday, May 1, 2012

Android: How to refer to the current activity inside of a listener.


Instead of this use YourActivityName.this where YourActivityName is the class name of your Activity subclass.
Explanation: You are creating an anonymous inner class when you use this part of your code: "new OnClickListener() {". Anonymous inner classes have a reference to the instance of the class they are created in. It looks like you are creating it inside an Activity subclass because findViewById is an Activity method. Activity's are a Context, so all you need to do is use the reference to it that you have automatically.
Refer to this link for more explanation.