Ninoop p george Ninoop p george - 23 days ago 10
Java Question

android : call a method with element as parameter

I am a newbie to android and java.
I have this

final Button mybutton = (Button) findViewById(R.id.mybutton);


How can i pass this
mybutton
into a method as a parameter. I mean the
mybutton
. so that i can use it inside a method to do something.

Like, i can call my method
hidethis()
as
classname.hidethis(mybutton)
and it hides the given object .
So, how can i declare the method with the element as the parameter?

Answer

Well, you'd better refer to documentation and Java tutorials.

But in your case, you can try:

public class VisibilityManager {
    public static void hide(View view){
        view.setVisibility(View.GONE); 
    }

    public static void show(View view){
        view.setVisibility(View.VISIBLE); 
    }
}

And then call this like:

final Button mybutton = (Button) findViewById(R.id.mybutton);
// you can do it because `Button extends View`.
VisibilityManager.hide(mybutton);

Read more: View#setVisibility(int)