Ryan
Ryan

Reputation: 10049

Android, Calling view object from code

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.
    }

Upvotes: 1

Views: 2950

Answers (4)

Bosah Chude
Bosah Chude

Reputation: 3800

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.

Upvotes: 1

Hiral Vadodaria
Hiral Vadodaria

Reputation: 19250

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.

Upvotes: 1

DeeV
DeeV

Reputation: 36035

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.

Upvotes: 3

Anju
Anju

Reputation: 9479

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.

Upvotes: 4

Related Questions