.NET Developer here just getting started with Eclipse and Android.
Can someone show me in the simplest way possible, with the absolute fewest lines of code, how to DO something when a button is clicked?
My button has id button1
and I just want to see where/how to write the onClick()
handler.
So let's say I have imageview1
set to invisible. How would I make it visible when the button is clicked?
EDIT:
Thanks everyone, but since not a single one of your examples work for me, I'll try this: Can someone please post the ENTIRE code to make this work? Not just the method, because when I try to use ANY of your methods I get errors all over the place so obviously something else is missing. I need to see everything, beginning with all the imports.
/src/com/example/MyClass.java
/res/layout/main.xml
You can just do it in your Activity's onCreate() method. For example:
Taken from: http://developer.android.com/guide/topics/ui/ui-events.html
Just declare a method,e.g:if ur button id is button1 then,
If you want to make the imageview1 visible then in that method write:
All answers are based on anonymous inner class. We have one more way for adding click event for buttons as well as other components too.
An activity needs to implement View.OnClickListener interface and we need to override the onClick function. I think this is best approach compared to using anonymous class.
Change your onCreate to
for me this update worked