Jump to content
  • Advertisement
Sign in to follow this  
Ibzy

Android app - basic button functionality

This topic is 1725 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All,

 

I am working on an Android app, and am just learning to program on Android.  I have experience in a few programming languages, but I'm struggling to combine functionality across xml and java.

 

Very simple question I'm sure, but how do I make a button activate a function?  I have: "public void function(string a, string b)" with the function inside in Java, and on the xml layout I have <Button android:onClick="function(stringa, stringb)".

 

When I click the button i get an error saying it cannot locate method "function(stringa, stringb)".

 

Thanks

Share this post


Link to post
Share on other sites
Advertisement

IIRC you have to specify a method that takes a View as its only parameter in the XML file.

 

so your method should be:

 

public void someMethod(View v) {

...code

}

 

and then in the xml layout:

 

<Button blaha blaha ....

android:onClick="someMethod" /> (only the method name, no parameters)

 

Your on click method also has to be a member of the current Activty.

Edited by SimonForsman

Share this post


Link to post
Share on other sites

Hi Simon,

 

Thanks for this - pointed me in the right direction.  Didn't quite realise how 'separated' the code needed to be.

 

Cheers

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!