Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does android first do layout?

Tags:

android

layout

I need to prepare some objects based on the initial layout of widgets and views in my Activity. I'd like to do it in the initialization steps, ideally onCreate and onStart. Considering the possibility of changes that happen when we're not in the foreground, some of them might need to happen in onResume.

So I checked whether I could measure how my views had been laid out.

Log.d("MyApp", "w,h of X is " + findViewById(R.id.X).getWidth() +
      "," + findViewById(R.id.X).getHeight());

I ran this in onCreate, onStart, and onResume. Each time I get 0,0 for width, height. If I wait for onTouchEvent I get the layout information, so the layout is done by then.

I'm surprised that the layout isn't set and final by the time I'm seeing onResume. I expected the widgets to be already laid out and ready to fiddle with by then.

like image 334
Brian Avatar asked Jan 21 '10 22:01

Brian


People also ask

What is the layout in Android?

A layout defines the structure for a user interface in your app, such as in an activity. All elements in the layout are built using a hierarchy of View and ViewGroup objects. A View usually draws something the user can see and interact with.

What is the default layout in Android application?

The Constraint layout is the default layout used in Android. It allows flexible positioning and sizing of child views. The layout uses the <ConstraintLayout> tag. In this layout, constraints are set on child views to avoid changing their position once the app is ran on a device or emulator.

Where are layouts placed in Android?

Layout files are stored in "res-> layout" in the Android application. When we open the resource of the application we find the layout files of the Android application. We can create layouts in the XML file or in the Java file programmatically. First, we will create a new Android Studio project named "Layouts Example".


1 Answers

Layout is done by the view hierarchy as needed.

When your app is starting, this will be some time after onResume() (as your window is being brought up and placed on the screen).

The correct way to find out about layout operations is through the various view hierarchy callbacks -- View.onSizeChanged() etc as documented under "Layout" here:

like image 160
hackbod Avatar answered Oct 03 '22 22:10

hackbod