1

I am kind of a newbie so excuse me if this question is too simple or too hard.

I have this code in my java file:

public void button_baby_clicked(View v) 
{
//do something here
}

this gets called when someone clicks the imagebutton in my xml file, but how do I call this from the java file itself? Because it's expecting a View object... and I'm guessing I need to recreate that? How?

Edit:
Ok, to clarify, I want to be able to call the above function via a click in my xml file as well as a function under it. For example:

    public void button_baby_clicked(View v) 
    {
    //do something here
    }

 public void someFunction() 
    {
    x = 10;
    button_baby_clicked(); // This should call the above function.
    }
Ryan
  • 9,821
  • 22
  • 66
  • 101
  • The easiest solution would be to do what the others have suggested. The method is a callback method. The OS will create the View object when it calls the method. The View object passed in will be the ImageView. – DeeV Oct 24 '11 at 12:36
  • The OS creates the View object when its called via the XML file (thats my understanding) but how do I create the view object if I am calling it directly from the same java file without using the XML file? – Ryan Oct 24 '11 at 12:46

4 Answers4

4

In ur ImageButton you have to add an attribute: android:onClick="button_baby_clicked" In the java file, you have added:

public void button_baby_clicked(View v) 
{
//do something here
}

The logic behind this is: Upon clicking ur imagebutton, this method will automatically get called, i.e "v" argument will be having ur imagebutton.

The advantage of giving like this is: You no need to initialize the imagebutton in ur activity and no need to set click listener too for this imagebutton.

Anju
  • 9,379
  • 14
  • 55
  • 94
3

Alright, if you want to have the method invoked every time the view is clicked, do what the others have said.

Alternatively, you can do something like this.

ImageView globalReference;

@Override
public void onCreate(Bundle icicle){
   *** CODE ***
   globalReference = (ImageView) findViewById(R.id.myImageView);
   *** CODE ***
}

Then, whenever you want that to be called with that particular View, simply call

button_baby_clicked(globalReference);

You can also do this with any View object you create dynamically.

View myTv = new TextView(context);
View myLl = new LinearLayout(context);

button_baby_clicked(myTv);
button_baby_clicked(myLl);

Just get a valid View reference within the same scope as the method, and pass it in like any other method. It can even be null if the method is capable of handling it.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • Thanks! Voted everyone up but yours is the answer I used so picked your answer. Have a good day guys and thanks again for replying to my call for help – Ryan Oct 24 '11 at 13:00
1

Can't you use it like -

mButton.setOnClickListener(new OnClickListener{

       @Override
       public void onClick(View v) {
                
           button_baby_clicked(v);
       }      
  }
);  

??

EDIT :

If you need to call someFunction() from the onClick of a button,and from there,you need to call button_baby_clicked(),you have to get View v object in someFunction. This link might help you. Please refer Start a service on onClick. You can change appropriately.

Community
  • 1
  • 1
Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • No, I want to call it from within the same java file, I know how to call it if someone clicks the button. – Ryan Oct 24 '11 at 12:29
  • something like if(x==y){button_baby_clicked();} – Ryan Oct 24 '11 at 12:30
  • @Ryan: that means,you need to check some condition. but i am still not getting where you want to use it? do you mean,you have this method in another java class and not in the class you button lies in? or something else? – Hiral Vadodaria Oct 24 '11 at 12:33
  • Edited my original post...please look. – Ryan Oct 24 '11 at 12:45
1

I believe its best if you refactor your code and put the code in the event handler into a global method that can be called from anywhere. like this:

public void button_baby_clicked(View v) 
{
  taskToPerform(); // Perform a certain task
}

public void someFunction() 
{
  x = 10;
  taskToPerform(), // Perform the same task again
}

public void taskToPerform()
{
   //This is where you write the task you want to perform
}

This way you can reuse the code in the taskToPerform() method anywhere, anytime.

Bosah Chude
  • 3,670
  • 2
  • 22
  • 22