Home > DeveloperSection > Forums > Code execution after activity creation in android
Lillian Martin

Total Post:27

Points:189
Posted on    November-19-2014 2:01 AM

 Android Java  Android Activity 
Ratings:


 1 Reply(s)
 600  View(s)
Rate this:

I want to execute some code immediately after the form is shown.

I want to check the size of a button on the form and use the same size to create a new button at runtime.

I tried onStart() and onResumed(), but they do not work.



Hugh Jackman
Hugh Jackman

Total Post:52

Points:366
Posted on    November-19-2014 7:18 AM

You can add a globallayout listener. Add the listener at onActivityCreated. Check this example:

button1.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

                @Override

                public void onGlobalLayout() {

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

                       button1.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                    } else {

                        button1.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                    }

                   //here the size is already available. create new button2 here with the size of button1

                }

            });


Don't want to miss updates? Please click the below button!

Follow MindStick