Home > OS >  How to update Button visibility from another class?
How to update Button visibility from another class?

Time:10-26

I want to call this function from another class.

How do I call it from another classs?.

It¡ss not possible to achieve using normal function call, as I am not getting access to the view of the main function.

 public class Blank  extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call);
        Button buttonHangup = (Button) findViewById(R.id.buttonHangup);
        Button buttonHanup2=(Button) findViewById(R.id.buttonHangup2);
        }
 
    public void bringBack(){

        Button buttonHangup = (Button) findViewById(R.id.buttonHangup);
        Button buttonHanup2=(Button) findViewById(R.id.buttonHangup2);
        buttonHangup.setText("Hangup no ok ");
        buttonHanup2.setVisibility(View.VISIBLE);

    }


}

how can i call this bringback function from another class as its not allowed to call using normal method call ...as i dont get access to button view from another class eg

 Blank bl =new Blank();
     bl.bringback()

it wont work as i dont get access to button view ..

CodePudding user response:

you can't call a graphical interface that way because blank extends from activity so you need to have the interaction with the layout.

What you could do is create a fragment with your own layout and embed it in different activities.

You could use a fragment that contains a button and this load it in your activities, for this you must create a framelayout within the layout of your activity where your fragment will be visible.

activity layout

enter image description here

Now you must create a fragment that contains a button

enter image description here

Now you must load this fragment in your activity (if at some point you don't need it anymore you can empty the framelayout or make it invisible)

enter image description here

In this way you can use the same button in different activities

CodePudding user response:

Here's an example that will hopefully help you out a little.

public class MyFunctionClass {

   public String myFunction() {
      return "This is an instance function.";
   }

   public static String myStaticFunction() {
      return "This is a static function.";
   }

}

Then in your activity you have something like this.

public class MyActivity extends Activity {

   @Override
   public void onCreate() {

      // If you want to call your static function, you do not
      // require an instance of a MyFunctionClass object.
      String myStaticString = MyFunctionClass.myStaticFunction();

      // If you want to call your instance function, then you need
      // to create a MyFunctionClass first.
      MyFunctionClass variableName = new MyFunctionClass();
      String myInstanceString = variableName.myFunction();
   }
}

Save yourself some frustration if you read up on object-oriented programming before diving in. There are some basic things that a new programmer will need to understand before diving in.

  • Related