9

I would like to perform some task after the creation of the graphic interface of the activity. I need to know the exact height and width of some views and change the layoutParams of some of those views based on the width and height. In the onResume method the views have all the parameters still equal to 0...

As for now I'm using a delayed task that runs after some time from the onCreate but this isn't a good solution at all...

What is the last method called in the activity creation? And are the views' width and height available in such method?

aveschini
  • 1,632
  • 3
  • 22
  • 39

5 Answers5

13

Call this inside of the onCreate()

       final View rootView = getWindow().getDecorView().getRootView();
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {

                        //by now all views will be displayed with correct values

                    }
                });
A. Adam
  • 3,094
  • 1
  • 26
  • 25
4

onResume() is last, but perhaps better is onViewCreated(). Its advantage is that it is not invoked every time you regain focus. But try getting properties of your view inside of post() over layout element which you need. For example:

        textView.post(new Runnable() {
            @Override
            public void run() {
                 // do something with textView
            }
        });
Malachiasz
  • 7,126
  • 2
  • 35
  • 49
  • Thanks, I'll try both! But why is the second better than the first in your opinion? – aveschini Nov 04 '13 at 10:14
  • no you should use my code inside of onViewCreated. post() is needed because " The fragment's view hierarchy is not however attached to its parent at this point." And if it still doesn't return proper result then use postDelayed(). It's dirty, but simple and works. – Malachiasz Nov 04 '13 at 10:16
  • and here you have the same problem: http://stackoverflow.com/questions/4074937/android-how-to-get-a-custom-views-height-and-width – Malachiasz Nov 04 '13 at 10:24
  • It's not exactly the same because I need to modify the layout of some views based on the parameters of other views that aren't even siblings in the same layout – aveschini Nov 04 '13 at 10:56
  • 2
    There is no method called onViewCreated(). There is onCreateView() but it is called before views have inflated and not after. – Price Feb 11 '15 at 20:21
  • Acitivity does no have onViewCreated. Fragments have it. – Javier Delgado Feb 03 '17 at 08:39
1

The last method that runs when activity starts is onResume(). You can find it at Activity lifecycle.

If that is not good enough for you, run delayed task from this onResume() and you'll be fine.

Yaniv
  • 3,321
  • 8
  • 30
  • 45
0

In the last line of the onResume() method get all the data you want. It should show you all that you need.

Hitman
  • 588
  • 4
  • 10
0

Use a Coroutine:

In onCreate() ...

CoroutineScope(SupervisorJob()).launch {
  getImageAttributes()
}

... then use a while loop to wait until image is in View ...

private fun getImageAttributes() {
  while (imageView.height == 0) { /* */ }
  val h = imageView.height
  val w = imageView.width
  val b = imageView.clipBounds
  val x = imageView.x
  val y = imageView.y
  val tx = imageView.translationX
  val ty = imageView.translationY
}

You should add a timeout or make the Job cancellable if there is a chance that the imageView.height is going to be 0.

Kent
  • 383
  • 3
  • 5