{"id":6345,"date":"2019-08-08T20:44:22","date_gmt":"2019-08-08T20:44:22","guid":{"rendered":"https:\/\/cheesecakelabs.com\/blog\/?p=6345\/"},"modified":"2022-07-01T17:15:12","modified_gmt":"2022-07-01T17:15:12","slug":"understanding-android-views-dimensions-set","status":"publish","type":"post","link":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/","title":{"rendered":"Understanding when and how Android Views have dimensions set"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">I believe that almost every Android dev has at least once tried to get a View\u2019s dimension, but it returned 0, <\/span><i><span style=\"font-weight: 400;\">nada<\/span><\/i><span style=\"font-weight: 400;\">. This is a very common situation, it is one of those problems that we just Google for a solution and find a workaround that seems to work. We add it to the code without having much idea of what\u2019s going on and that\u2019s it. In this article I\u2019ll explain why this happens and why many of the most used workarounds should be avoided. In the end, I\u2019ll give you a very sweet solution about Android views.<\/span><\/p>\n<p><!--more--><\/p>\n<h1><span style=\"font-weight: 400;\">Views dimension in Activity lifecycle&nbsp;<\/span><\/h1>\n<p><span style=\"font-weight: 400;\">One of the very basic Android concepts that we first learn is the Activity lifecycle. We understand that the <\/span><span style=\"font-weight: 400;\">onCreate()<\/span><span style=\"font-weight: 400;\"> callback is where the UI should be declared and configured. Having this in mind, let\u2019s say we have a View that for some reason needs a manual setup like this:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\">myView.layoutParams.width = myView.width \/ 2<\/code><\/pre>\n<p><span style=\"font-weight: 400;\">This sets <\/span><span style=\"font-weight: 400;\">myView<\/span><span style=\"font-weight: 400;\"> width based on its own width, this means that this code should run only once. You add it inside <\/span><span style=\"font-weight: 400;\">onCreate()<\/span><span style=\"font-weight: 400;\"> right after setting content view, run the application but it doesn\u2019t work as expected. <\/span><span style=\"font-weight: 400;\">myView<\/span><span style=\"font-weight: 400;\"> had a width of zero and then your code just divided 0 by 2. So you think, maybe the view will be laid out in <\/span><span style=\"font-weight: 400;\">onStart()<\/span><span style=\"font-weight: 400;\">. You test it and again it doesn\u2019t work. Maybe <\/span><span style=\"font-weight: 400;\">onResume()<\/span><span style=\"font-weight: 400;\"> will work? Wait&#8230; what?! <\/span><span style=\"font-weight: 400;\">onResume<\/span><span style=\"font-weight: 400;\"> doesn\u2019t work either?! The next lifecycle step should be when the Activity is already running and you are still unable to finish the UI setup. What else can we do?<\/span><\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-6356\" src=\"https:\/\/s3.amazonaws.com\/ckl-website-static\/wp-content\/uploads\/2019\/08\/meme-nazare-fotoreproducao-via-google_2094781-e1565296766341.jpg\" alt=\"A meme of a blond woman thinking with some formular drawn on her picture.\" width=\"420\" height=\"238\"><\/p>\n<p><span style=\"font-weight: 400;\">Ok, let\u2019s be radical and try <\/span><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity#onWindowFocusChanged(boolean)\"><span style=\"font-weight: 400;\">onWindowsFocusChanged()<\/span><\/a><span style=\"font-weight: 400;\">. <\/span><i><span style=\"font-weight: 400;\">Voil\u00e0<\/span><\/i><span style=\"font-weight: 400;\">, it works! You are happy and can go back to work. Suddenly your app goes to background and a phone call pops up, it\u2019s your mom asking you to buy some bread on your way home. After the call ends, your app comes back to the foreground, as well as your frustration. <\/span><span style=\"font-weight: 400;\">myView<\/span><span style=\"font-weight: 400;\"> has now half the width that it should, and you realize that <\/span><span style=\"font-weight: 400;\">onWindowsFocusChanged()<\/span><span style=\"font-weight: 400;\"> was called again after the app came back to the foreground.&nbsp;<\/span><\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-6346 size-full\" src=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/gifdiego1resized.gif\" alt=\"Gif of Jim Carrey drinking a glass of water and spitting it out.\" width=\"280\" height=\"304\"><\/p>\n<p><span style=\"font-weight: 400;\">We now know that for some reason there isn\u2019t a lifecycle callback for when the activity UI is fully laid out, which is something that some stacks provide for us, such as on iOS. So the question is, when is a view fully laid out and drawn?<\/span><\/p>\n<h1><span style=\"font-weight: 400;\">How Android draws the UI<\/span><\/h1>\n<p><span style=\"font-weight: 400;\">An Activity layout is drawn upon the view hierarchy. From the image below, you can see that this hierarchy is a tree with two different nodes &#8211; ViewGroup and View.<\/span><\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-6347 aligncenter\" src=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/image-7.png\" alt=\"View group's hierarchy picture.\" width=\"474\" height=\"253\"><\/p>\n<p><span style=\"font-weight: 400;\">ViewGroup is also a View, but it is a layout or container that can contain many children. Examples of ViewGroups are LinearLayout and ConstraintLayout. View is any other UI component.&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Each View has two different types of sizes: the measured width and height, which is basically how big it wants to be, and the width and height that are the actual dimensions they have after being laid out. The desired size cannot always be provided, e.g. a view could request a width larger than its parent\u2019s width.&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Both measured and actual sizes are set in two steps:&nbsp;<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><b>Measure pass<\/b><span style=\"font-weight: 400;\">: each View has a desired size &#8211; a measured dimension that is basically how big it wants to be. The measure pass is a process that starts from the root View Group and goes in a top-down transversal way setting the measured width and height of each View based on requirements passed from parent to children. Each View returns to its descendants the desired dimension. When this phase is over, every View has both measured width and height set.<\/span><\/li>\n<li style=\"font-weight: 400;\"><b>Layout pass<\/b><span style=\"font-weight: 400;\">: also top-down transversal, each parent is responsible for positioning its children Views based on actual sizes, which are not always the measured sizes set on Measure pass &#8211; every View has a measured width and height.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">After the layout pass finishes, the layout can finally be drawn by the OS.&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This is a very basic explanation, but it is enough to have an idea of how the draw process works. But when does it happen? The <\/span><a href=\"https:\/\/developer.android.com\/guide\/topics\/ui\/how-android-draws\"><span style=\"font-weight: 400;\">documentation<\/span><\/a><span style=\"font-weight: 400;\"> states that it starts when the Activity receives focus, then let\u2019s see it happening with a small code that I wrote.&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">The code has a custom LinearLayout and TextView which I created just to override <\/span><span style=\"font-weight: 400;\">onMeasure()<\/span><span style=\"font-weight: 400;\">, <\/span><span style=\"font-weight: 400;\">onLayout()<\/span><span style=\"font-weight: 400;\">\/<\/span><span style=\"font-weight: 400;\">layout()<\/span><span style=\"font-weight: 400;\"> and <\/span><span style=\"font-weight: 400;\">draw()<\/span><span style=\"font-weight: 400;\"> functions and add some logs to them. You can check it out in this <\/span><a href=\"https:\/\/gist.github.com\/diegohkd\/06d35a67a4d16f18c60fb0785381704d\"><span style=\"font-weight: 400;\">Gist<\/span><\/a><span style=\"font-weight: 400;\">.&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Running it results in these logs:<\/span><\/p>\n<pre><span style=\"font-weight: 400;\">1 onCreate() executed<\/span>\n\n<span style=\"font-weight: 400;\">2 onStart() executed<\/span>\n\n<span style=\"font-weight: 400;\">3 onResume() executed<\/span>\n\n<span style=\"font-weight: 400;\">4 LinearLayout: entering onMeasure(). Measured width: 0<\/span>\n\n<span style=\"font-weight: 400;\">5 TextView: entering onMeasure(). Measured width: 0<\/span>\n\n<span style=\"font-weight: 400;\">6 TextView: leaving onMeasure(). Measured width: 171<\/span>\n\n<span style=\"font-weight: 400;\">7 LinearLayout: leaving onMeasure(). Measured width: 171<\/span>\n\n<span style=\"font-weight: 400;\">8 LinearLayout: entering onLayout(). Actual width: 171<\/span>\n\n<span style=\"font-weight: 400;\">9 TextView: entering layout(). Actual width: 0<\/span>\n\n<span style=\"font-weight: 400;\">10 TextView: leaving layout(). Actual width: 171<\/span>\n\n<span style=\"font-weight: 400;\">11 LinearLayout: leaving onLayout(). Actual width: 171<\/span>\n\n<span style=\"font-weight: 400;\">12 onWindowFocusChanged() executed<\/span>\n\n<span style=\"font-weight: 400;\">13 LinearLayout: entering onMeasure(). Measured width: 171<\/span>\n\n<span style=\"font-weight: 400;\">14 TextView: entering onMeasure(). Measured width: 171<\/span>\n\n<span style=\"font-weight: 400;\">15 TextView: leaving onMeasure(). Measured width: 171<\/span>\n\n<span style=\"font-weight: 400;\">16 LinearLayout: leaving onMeasure(). Measured width: 171<\/span>\n\n<span style=\"font-weight: 400;\">17 LinearLayout: entering onLayout(). Actual width: 171<\/span>\n\n<span style=\"font-weight: 400;\">18 TextView: entering layout(). Actual width: 171<\/span>\n\n<span style=\"font-weight: 400;\">19 TextView: leaving layout(). Actual width: 171<\/span>\n\n<span style=\"font-weight: 400;\">20 LinearLayout: leaving onLayout(). Actual width: 171<\/span>\n\n<span style=\"font-weight: 400;\">21 TextView: draw() executed<\/span><\/pre>\n<p><span style=\"font-weight: 400;\">We can perfectly see measure and layout passes happening from lines 1 to 11, with measured and actual sizes of both views going from 0 to 171.<\/span> We can also notice that the two passes happen twice, once before onWindowFocusChanged()and again from lines 13 to 21, but TextView calls draw() only when the Activity gets focus. This means that the layout is indeed drawn only after the Activity receives focus, but the views are laid out before it.<\/p>\n<p><span style=\"font-weight: 400;\">Understanding this whole process is very helpful, but it\u2019s still hard to know how to have a callback for when the entire screen is laid out.<\/span><\/p>\n<h1><span style=\"font-weight: 400;\">Most common solutions<\/span><\/h1>\n<p><span style=\"font-weight: 400;\">After doing a little research, you will probably find some solutions that people claim they work, which usually are:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/View.html#post(java.lang.Runnable)\"><b>View.post(Runnable action)<\/b><\/a><span style=\"font-weight: 400;\">: this can be used to send your UI setup code to the message queue. Adding this after calling<\/span><span style=\"font-weight: 400;\"> setContentView()<\/span><span style=\"font-weight: 400;\"> inside <\/span><span style=\"font-weight: 400;\">onCreate()<\/span><span style=\"font-weight: 400;\"> usually makes your code run after the UI is laid out. But you don\u2019t have much control of message queue, which means this approach is much more a workaround than an elegant solution that we know will always work.<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/ViewTreeObserver.OnGlobalLayoutListener\"><b>ViewTreeObserver.OnGlobalLayoutListener<\/b><\/a><span style=\"font-weight: 400;\">: with this listener, you can set up a code that will be executed when global layout changes happen in the whole view tree. There\u2019s a nice <\/span><a href=\"https:\/\/antonioleiva.com\/kotlin-ongloballayoutlistener\/\"><span style=\"font-weight: 400;\">article<\/span><\/a><span style=\"font-weight: 400;\"> with its usage explained by Antonio Leiva. But in the article there\u2019s also a tweet from Chris Banes where he explains that this approach is overkill because it is triggered when layout changes happened to any view in the tree, and even with some improvements, such as removing the listener after the first call, it\u2019s not guaranteed that when the listener is called your view has already been laid out, i.e. it might not have width and height set yet.<\/span><\/li>\n<li style=\"font-weight: 400;\"><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/View.OnLayoutChangeListener\"><b>View.OnLayoutChangeListener<\/b><\/a><span style=\"font-weight: 400;\">:&nbsp;<\/span><span style=\"font-weight: 400;\">\u201cInterface definition for a callback to be invoked when the layout bounds of a view changes due to layout processing.\u201d<\/span><span style=\"font-weight: 400;\">This looks promising. It will be triggered only when a specific view is changed instead of the whole view tree. But what if at the time that this listener is added, this view is already laid out and then it never runs your code? In this case, we could make it run the code straight away. We could even write a Kotlin extension for this, right? Nah, no need, it\u2019s already done and it\u2019s in the <\/span><a href=\"https:\/\/developer.android.com\/jetpack\/?gclid=CjwKCAjw98rpBRAuEiwALmo-yucQWH7JJn4CtwH8j2OKJwYU7ObSXu7k8VSH_kvUL34AaK-1nTauthoCOOEQAvD_BwE\"><span style=\"font-weight: 400;\">Android Jetpack<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ul>\n<h1><span style=\"font-weight: 400;\">Android KTX and doOnLayout to the rescue<\/span><\/h1>\n<p><a href=\"https:\/\/developer.android.com\/kotlin\/ktx.html\"><span style=\"font-weight: 400;\">Android KTX<\/span><\/a><span style=\"font-weight: 400;\"> is a library that is part of Android Jetpack and has a lot of useful Kotlin extensions. For our case, it has the <\/span><a href=\"https:\/\/developer.android.com\/reference\/kotlin\/androidx\/core\/view\/package-summary#doonlayout\"><span style=\"font-weight: 400;\">View.doOnLayout()<\/span><\/a><span style=\"font-weight: 400;\"> extension with a callback for when a view is laid out, and it also ensures that the callback runs even if the View has already been laid out and hasn\u2019t requested layout. If you are wondering what\u2019s the magic behind it, there isn\u2019t. It uses View.OnLayoutChangeListener without any crazy logic, as you can see in the <\/span><a href=\"https:\/\/android.googlesource.com\/platform\/frameworks\/support\/+\/android-room-release\/core\/ktx\/src\/main\/java\/androidx\/core\/view\/View.kt\"><span style=\"font-weight: 400;\">source code<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><img decoding=\"async\" class=\"alignnone size-full wp-image-6348 aligncenter\" src=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/gifdiego2.gif\" alt=\"A gif of Steve Carell slapping a table and saying 'thank you'.\" width=\"350\" height=\"194\"><\/span><\/p>\n<h1><span style=\"font-weight: 400;\">A lifecycle callback for when the entire layout is laid out<\/span><\/h1>\n<p><span style=\"font-weight: 400;\">With <\/span><span style=\"font-weight: 400;\">doOnLayout()<\/span><span style=\"font-weight: 400;\"> we have a callback for when a specific view is laid out, but what if you want a callback that is called when the entire layout is laid out? Well, we know that the root View is the last one to be laid out, so this means that by using <\/span><span style=\"font-weight: 400;\">doOnLayout()<\/span><span style=\"font-weight: 400;\"> with the root View, it will be called when all other Views have already been laid out. There\u2019s just a small problem: how to get the root View?&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To achieve this goal in a generic way, we could get the content view of top-level window decor using a base Activity class:<\/span><\/p>\n<pre class=\"language-swift\"><code class=\"language-swift\"><span style=\"font-weight: 400;\">abstract class BaseActivity: AppCompatActivity() {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;protected open fun doOnRootViewLayout(action: () -&gt; Unit) {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;val rootView = window.decorView.findViewById&lt;View&gt;(android.R.id.content)<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rootView.doOnLayout {<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action()<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<\/span>\n\n<span style=\"font-weight: 400;\">&nbsp;&nbsp;&nbsp;&nbsp;}<\/span>\n\n<span style=\"font-weight: 400;\">}<\/span>\n\n<\/code><\/pre>\n<p><span style=\"font-weight: 400;\">Unfortunately I couldn\u2019t find much information about this approach and how safe it is, except an <\/span><a href=\"https:\/\/android-developers.googleblog.com\/2009\/03\/window-backgrounds-ui-speed.html\"><span style=\"font-weight: 400;\">article<\/span><\/a><span style=\"font-weight: 400;\"> explaining that this low-level layer can change in other Android versions or devices. Also on Android documentation there isn\u2019t much information about <\/span><a href=\"https:\/\/developer.android.com\/reference\/android\/R.id#content\"><span style=\"font-weight: 400;\">android.R.id.content<\/span><\/a><span style=\"font-weight: 400;\">.&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">My suggestion is to give an ID to the root view of your layout and use this view to call <\/span><span style=\"font-weight: 400;\">doOnLayout()<\/span><span style=\"font-weight: 400;\">, which also works with Fragments and custom views.<\/span><\/p>\n<h1><span style=\"font-weight: 400;\">Wrapping up<\/span><\/h1>\n<p>We were able to understand when Views have dimensions set and are drawn. For some reason there is not a lifecycle callback for that, but at least we came up with a working solution using doOnLayout.<\/p>\n<p>Working with Android is a mix of love and hate, we all know that. Sometimes very basic things are difficult to achieve or understand (and it is even harder to understand why they are not explained in the documentation). I hope this article helps you to diminish the hate moments caused by some Android flaws (although it might have added more questions than answers) and write a better code and UI without fear of all that obscurity that Android sometimes leaves to us.<\/p>\n<h1><span style=\"font-weight: 400;\">References<\/span><\/h1>\n<p><a href=\"https:\/\/developer.android.com\/guide\/components\/activities\/activity-lifecycle\"><span style=\"font-weight: 400;\">https:\/\/developer.android.com\/guide\/components\/activities\/activity-lifecycle<\/span><\/a><\/p>\n<p><a href=\"https:\/\/developer.android.com\/reference\/android\/app\/Activity#onWindowFocusChanged(boolean)\"><span style=\"font-weight: 400;\">https:\/\/developer.android.com\/reference\/android\/app\/Activity#onWindowFocusChanged(boolean)<\/span><\/a><\/p>\n<p><a href=\"https:\/\/developer.android.com\/guide\/topics\/ui\/how-android-draws\"><span style=\"font-weight: 400;\">https:\/\/developer.android.com\/guide\/topics\/ui\/how-android-draws<\/span><\/a><\/p>\n<p><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/View.html#post(java.lang.Runnable)\"><span style=\"font-weight: 400;\">https:\/\/developer.android.com\/reference\/android\/view\/View.html#post(java.lang.Runnable)<\/span><\/a><\/p>\n<p><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/ViewTreeObserver.OnGlobalLayoutListener\"><span style=\"font-weight: 400;\">https:\/\/developer.android.com\/reference\/android\/view\/ViewTreeObserver.OnGlobalLayoutListener<\/span><\/a><\/p>\n<p><a href=\"https:\/\/antonioleiva.com\/kotlin-ongloballayoutlistener\/\"><span style=\"font-weight: 400;\">https:\/\/antonioleiva.com\/kotlin-ongloballayoutlistener\/<\/span><\/a><\/p>\n<p><a href=\"https:\/\/developer.android.com\/reference\/android\/view\/View.OnLayoutChangeListener\"><span style=\"font-weight: 400;\">https:\/\/developer.android.com\/reference\/android\/view\/View.OnLayoutChangeListener<\/span><\/a><\/p>\n<p><a href=\"https:\/\/developer.android.com\/jetpack\/\"><span style=\"font-weight: 400;\">https:\/\/developer.android.com\/jetpack\/<\/span><\/a><\/p>\n<p><a href=\"https:\/\/developer.android.com\/kotlin\/ktx\"><span style=\"font-weight: 400;\">https:\/\/developer.android.com\/kotlin\/ktx<\/span><\/a><\/p>\n<p><a href=\"https:\/\/developer.android.com\/reference\/kotlin\/androidx\/core\/view\/package-summary#doonlayout\"><span style=\"font-weight: 400;\">https:\/\/developer.android.com\/reference\/kotlin\/androidx\/core\/view\/package-summary#doonlayout<\/span><\/a><\/p>\n<p><a href=\"https:\/\/android.googlesource.com\/platform\/frameworks\/support\/+\/android-room-release\/core\/ktx\/src\/main\/java\/androidx\/core\/view\/View.kt\"><span style=\"font-weight: 400;\">https:\/\/android.googlesource.com\/platform\/frameworks\/support\/+\/android-room-release\/core\/ktx\/src\/main\/java\/androidx\/core\/view\/View.kt<\/span><\/a><\/p>\n<p><a href=\"https:\/\/android-developers.googleblog.com\/2009\/03\/window-backgrounds-ui-speed.html\"><span style=\"font-weight: 400;\">https:\/\/android-developers.googleblog.com\/2009\/03\/window-backgrounds-ui-speed.html<\/span><\/a><\/p>\n<p><a href=\"https:\/\/developer.android.com\/reference\/android\/R.id#content\"><span style=\"font-weight: 400;\">https:\/\/developer.android.com\/reference\/android\/R.id#content<\/span><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I believe that almost every Android dev has at least once tried to get a View\u2019s dimension, but it returned 0, nada. This is a very common situation, it is one of those problems that we just Google for a solution and find a workaround that seems to work. We add it to the code [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":6349,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[432,1162],"tags":[299],"class_list":["post-6345","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-engineering","category-process","tag-tag-mobile-development"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Understanding when and how Android Views have dimensions set<\/title>\n<meta name=\"description\" content=\"Working with Android views is a mix of love and hate, we all know that. This article will help you to write better code without fear. Click to learn more.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Understanding when and how Android Views have dimensions set\" \/>\n<meta property=\"og:description\" content=\"Working with Android views is a mix of love and hate, we all know that. This article will help you to write better code without fear. Click to learn more.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/\" \/>\n<meta property=\"og:site_name\" content=\"Cheesecake Labs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cheesecakelabs\" \/>\n<meta property=\"article:published_time\" content=\"2019-08-08T20:44:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-01T17:15:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/Blogpost-Diego.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2000\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Cheesecake Labs\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cheesecakelabs\" \/>\n<meta name=\"twitter:site\" content=\"@cheesecakelabs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/\"},\"author\":{\"name\":\"Diego Oliveira\"},\"headline\":\"Understanding when and how Android Views have dimensions set\",\"datePublished\":\"2019-08-08T20:44:22+00:00\",\"dateModified\":\"2022-07-01T17:15:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/\"},\"wordCount\":1705,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/Blogpost-Diego.png\",\"keywords\":[\"mobile development\"],\"articleSection\":[\"Engineering\",\"Process\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/\",\"url\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/\",\"name\":\"Understanding when and how Android Views have dimensions set\",\"isPartOf\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/Blogpost-Diego.png\",\"datePublished\":\"2019-08-08T20:44:22+00:00\",\"dateModified\":\"2022-07-01T17:15:12+00:00\",\"author\":{\"@type\":\"person\",\"name\":\"Diego Oliveira\"},\"description\":\"Working with Android views is a mix of love and hate, we all know that. This article will help you to write better code without fear. Click to learn more.\",\"breadcrumb\":{\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#primaryimage\",\"url\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/Blogpost-Diego.png\",\"contentUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/Blogpost-Diego.png\",\"width\":2000,\"height\":720,\"caption\":\"android views\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/cheesecakelabs.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Understanding when and how Android Views have dimensions set\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#website\",\"url\":\"https:\/\/cheesecakelabs.com\/blog\/\",\"name\":\"Cheesecake Labs\",\"description\":\"Nearshore outsourcing company for Web and Mobile design and engineering services, and staff augmentation for startups and enterprises..\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/cheesecakelabs.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"name\":\"Diego Oliveira\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/cheesecakelabs.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2017\/06\/diego-300x300.jpg\",\"contentUrl\":\"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2017\/06\/diego-300x300.jpg\",\"caption\":\"Diego Oliveira\"},\"description\":\"10 years of experience in Marketing and Sales in the Technology sector. My main purpose is help, support and structure efficient operations and also develop independent and multidisciplinary teams.\",\"url\":\"https:\/\/cheesecakelabs.com\/blog\/autor\/diegooliveira\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Understanding when and how Android Views have dimensions set","description":"Working with Android views is a mix of love and hate, we all know that. This article will help you to write better code without fear. Click to learn more.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/","og_locale":"en_US","og_type":"article","og_title":"Understanding when and how Android Views have dimensions set","og_description":"Working with Android views is a mix of love and hate, we all know that. This article will help you to write better code without fear. Click to learn more.","og_url":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/","og_site_name":"Cheesecake Labs","article_publisher":"https:\/\/www.facebook.com\/cheesecakelabs","article_published_time":"2019-08-08T20:44:22+00:00","article_modified_time":"2022-07-01T17:15:12+00:00","og_image":[{"width":2000,"height":720,"url":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/Blogpost-Diego.png","type":"image\/png"}],"author":"Cheesecake Labs","twitter_card":"summary_large_image","twitter_creator":"@cheesecakelabs","twitter_site":"@cheesecakelabs","twitter_misc":{"Written by":null,"Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#article","isPartOf":{"@id":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/"},"author":{"name":"Diego Oliveira"},"headline":"Understanding when and how Android Views have dimensions set","datePublished":"2019-08-08T20:44:22+00:00","dateModified":"2022-07-01T17:15:12+00:00","mainEntityOfPage":{"@id":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/"},"wordCount":1705,"commentCount":0,"image":{"@id":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#primaryimage"},"thumbnailUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/Blogpost-Diego.png","keywords":["mobile development"],"articleSection":["Engineering","Process"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/","url":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/","name":"Understanding when and how Android Views have dimensions set","isPartOf":{"@id":"https:\/\/cheesecakelabs.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#primaryimage"},"image":{"@id":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#primaryimage"},"thumbnailUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/Blogpost-Diego.png","datePublished":"2019-08-08T20:44:22+00:00","dateModified":"2022-07-01T17:15:12+00:00","author":{"@type":"person","name":"Diego Oliveira"},"description":"Working with Android views is a mix of love and hate, we all know that. This article will help you to write better code without fear. Click to learn more.","breadcrumb":{"@id":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#primaryimage","url":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/Blogpost-Diego.png","contentUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2019\/08\/Blogpost-Diego.png","width":2000,"height":720,"caption":"android views"},{"@type":"BreadcrumbList","@id":"https:\/\/cheesecakelabs.com\/blog\/understanding-android-views-dimensions-set\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/cheesecakelabs.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Understanding when and how Android Views have dimensions set"}]},{"@type":"WebSite","@id":"https:\/\/cheesecakelabs.com\/blog\/#website","url":"https:\/\/cheesecakelabs.com\/blog\/","name":"Cheesecake Labs","description":"Nearshore outsourcing company for Web and Mobile design and engineering services, and staff augmentation for startups and enterprises..","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/cheesecakelabs.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","name":"Diego Oliveira","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/cheesecakelabs.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2017\/06\/diego-300x300.jpg","contentUrl":"https:\/\/ckl-website-static.s3.amazonaws.com\/wp-content\/uploads\/2017\/06\/diego-300x300.jpg","caption":"Diego Oliveira"},"description":"10 years of experience in Marketing and Sales in the Technology sector. My main purpose is help, support and structure efficient operations and also develop independent and multidisciplinary teams.","url":"https:\/\/cheesecakelabs.com\/blog\/autor\/diegooliveira\/"}]}},"_links":{"self":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts\/6345","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/comments?post=6345"}],"version-history":[{"count":1,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts\/6345\/revisions"}],"predecessor-version":[{"id":10200,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/posts\/6345\/revisions\/10200"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/media\/6349"}],"wp:attachment":[{"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/media?parent=6345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/categories?post=6345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cheesecakelabs.com\/blog\/wp-json\/wp\/v2\/tags?post=6345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}