Sign in to follow this  

Android app - basic button functionality

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)".



Share this post

Link to post
Share on other sites

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) {




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.



Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this