2

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.

Thanks, Ashok

  • I think you are not looking for when the activity is created, but the layout is built. I have not dealt with that, but [this](http://stackoverflow.com/questions/8418868/how-to-know-when-an-activity-finishes-a-layout-pass) seems a good place to start. – MalaKa Sep 08 '14 at 16:29
  • You can post a `Runnable` on it. [See this answer](http://stackoverflow.com/questions/18283686/listview-footer-at-the-bottom-of-screen/18283819#18283819) – codeMagic Sep 08 '14 at 16:33

2 Answers2

3

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
                }
            });
Pedro Oliveira
  • 20,371
  • 8
  • 53
  • 82
0

you are looking at the onCreate() method, that is the function that will run when your app is first loaded in to memory

mpop
  • 490
  • 11
  • 21