1

I am new to Android development and Eclipse. I have been coding on ASP.Net and MS Visual Web Developer for years. In VWD, when you add a control to the design view, double clicking on it will automatically bring you to code view for the OnClick function of the control you have just created. You can also see the list of possible event handlers for a control from the design view.

But I can't seem to find this feature in Eclipse. Is there such a thing? I did a search on Google and the best I found is this (same question but without an answer).

http://www.techrepublic.com/forum/questions/101-341077/event-handlers-in-eclipse

Anyone to advice please?

Thanks!

Jason
  • 13
  • 4

2 Answers2

0

No, that is not how Eclipse works. You add the control in the xml file, then in the activity that you are going to load that layout in you add the onClickListener on the element you want to respond to clicks for,

Kaediil
  • 5,465
  • 2
  • 21
  • 20
0

What you're talking doesn't quite exist in Eclipse. You'll have to manually open your java class and add the method to the corresponding java activity there.

For example, if you set the android:onClick XML attribute to "myAwesomeMethod" in your layout XML file, in the corresponding Activity that uses that layout, you'll need to make sure you have a "myAwesomeMethod" method defined.

<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="@+id/mybutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click me!"
    android:onClick="myAwesomeMethod" />
<!-- even more layout elements -->

In your java Activity class:

public void myAwesomeMethod(View v) {
    // does something very awesome
}

Note: you can also do this programmatically, which is what I generally do. However, defining the android:onClick method will save you a few lines of code.

For more information, check out this post. It gives a lot more detail on how to assign onClick handlers to a button and the two ways you can do so.

Community
  • 1
  • 1
Kyle Clegg
  • 38,547
  • 26
  • 130
  • 141
  • 1
    Thanks Kyle for the great example and the link. At least now I know how this works in Eclipse! – Jason Jul 25 '12 at 18:41
  • Glad to help. One additional note after I've given this a little more thought: One reason that you can't go directly from a button in the GUI editor to the corresponding java code is because that particular button may be reused between 2, 10, or 100 Android Activities. Your XML layouts can all be reused which actually gives you a decent level of flexibility. Rather than needing to copy your UI files like you may need to do on other platforms, you can simply reuse the same layout XML and change a few things in the code if necessary. – Kyle Clegg Jul 25 '12 at 22:40