5

I have a Button and it is defined in XML. I am using android:onClick to call a method called showMessage. Simple method example:

    public void showMessage(View v){
    Log.w(TAG, "Hi");
    }

Now suppose my method needs for example a boolean and an int as parameters, how to do something like:

android:onClick="showMessage(boolean isExpensive, int money)"

taxo
  • 539
  • 1
  • 3
  • 21
  • It does not work that way AFAIK. Using a callback from the layout's XML is limited. If you want something more complex, simply on `View.setOnClickListener()`. – shkschneider Nov 28 '14 at 15:58
  • You Cannot do it. Onclick is the inbuild method of android which has only one param that is clicked view. – MathanG Nov 28 '14 at 15:58
  • possible duplicate of [How to pass parameters to OnClickListener?](http://stackoverflow.com/questions/10614696/how-to-pass-parameters-to-onclicklistener). A **brilliant solution** is shown. – Phantômaxx Nov 28 '14 at 15:58
  • Thanks for the comments, I knew I couldn't do it, i was just looking for a work around. @DerGolem I understand the example if I had to call my method in the code, but can't quite get how to use it in my XML situation. Can you be more specific please? :) – taxo Nov 28 '14 at 16:18
  • 1
    Exactly as @yaa110 showed you- – Phantômaxx Nov 28 '14 at 16:21

3 Answers3

12

It is not possible to pass parameters as you did, but you can use tags:

<Button 
    android:id="@+id/btn1"
    android:tag="false,25"
    android:onClick="showMessage"
/>

<Button 
    android:id="@+id/btn2"
    android:tag="true,50"
    android:onClick="showMessage"
/>

and in your java:

public void showMessage(View v) {
    String tag = v.getTag().toString();
    boolean isExpensive = Boolean.parseBoolean(tag.split(",")[0]);
    int money = Integer.parseInt(tag.split(",")[1]);
    this.showMessage(isExpensive, money);
}

public void showMessage(boolean isExpensive, int money) {
    // Your codes here
}
  • This may actually do the trick, I will try it and will give some feedback tomorrow :) Thanks in advance! – taxo Nov 28 '14 at 16:14
1

Wouldn't it be easier to use an onclicklistener?

Define the button with id:

        <Button
        android:id="@+id/button1"
        /.. more attributes here ../
        android:text="@string/something" />

and in your activity:

Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {          
    @Override
    public void onClick(View v) {
        Log.w(TAG, "Hi");
        }});
Jeppe
  • 1,830
  • 3
  • 24
  • 33
0

You can do it this way: Define another function in the activity.java without the argument. Call this function from the button click.

Make your actual function with the the argument you want to pass.

public void showMessageToClick(View v){
    // define your bool as per your need
    boolean isExpensive = true;
    int money=30000;

    showMessage(isExpensive, money)
    Log.w(TAG, "Hi");
} 

showMessage(boolean isExpensive, int money){
   // your code here
}
jepaljey
  • 15
  • 9