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,442
  • 8
  • 55
  • 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
  • 499
  • 11
  • 21
  • The layout information such as size won't be available yet – codeMagic Sep 08 '14 at 16:34
  • The layout will be avalable after you call the setContentView(R.layout.) – mpop Sep 08 '14 at 16:38
  • @mpop There will be access to the `Views` but no guarantee that they have been completely measured so the size, most likely, won't be available yet just because you call `setContentView()` – codeMagic Sep 08 '14 at 16:40