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

Total Post:27

Posted on    November-19-2014 2:01 AM

 Android Java  Android Activity 

 1 Reply(s)
 807  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

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() {


                public void onGlobalLayout() {

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


                    } else {



                   //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