Ninoop p george Ninoop p george - 25 days ago 9
Android Question

Call a method with element as parameter

I have this:

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


How can I pass
myButton
into a method as a parameter? So I can use it inside a method to do something.

Like, I can call my method
hideThis()
as
MyClass.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)