CODE EXECUTION AFTER ACTIVITY CREATION IN ANDROID

Lillian Martin

Total Post:27

Points:189
Posted by  Lillian Martin
 823  View(s)
Ratings:
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.

  1. Hugh Jackman

    Post:52

    Points:366
    Re: Code execution after activity creation in android

    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

                    }

                });

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!