What does android:layout_weight mean?
With layout_weight
you can specify a size ratio between multiple views. E.g. you have a MapView
and a table
which should show some additional information to the map. The map should use 3/4 of the screen and table should use 1/4 of the screen. Then you will set the layout_weight
of the map
to 3 and the layout_weight
of the table
to 1.
To get it work you also have to set the height or width (depending on your orientation) to 0px.
android- meaning of layout_weight=0
For all the view which have layout_weight must have layout_height or layout_width as 0dp depending on the orientation and requirement of layout.
layout_weight="1" and layout_width="0dp" ==> that particular view will be stretched horizontally if there is not other layout adjacent to it.
layout_weight="0" and layout_width="100dp" ==> that particular layout will behave as it is there is no meaning of layout_weight in this scenario.
Best use of weight is when you need two views having same height/width that are adjacent to each other you can add width/height as "0dp" for both layout and weight as "1" for both the layout.
what is the difference between layout_weight and using dp?
This highly depends on the exact use case.
layout_weight
depends on the number and size of the other views in the same ViewGroup.
dp (density-independant pixels)
depends on the density of the device.
Usually, dp is used to have a view displayed at the same physical size on devices with different screen densities, while weight just makes sure that a view fills a certain percentage of its parent ViewGroup.
what is the difference between layout_weight and using dp?
This highly depends on the exact use case.
layout_weight
depends on the number and size of the other views in the same ViewGroup.
dp (density-independant pixels)
depends on the density of the device.
Usually, dp is used to have a view displayed at the same physical size on devices with different screen densities, while weight just makes sure that a view fills a certain percentage of its parent ViewGroup.
What is android:weightSum in android, and how does it work?
Per documentation, android:weightSum
defines the maximum weight sum, and is calculated as the sum of the layout_weight
of all the children if not specified explicitly.
Let's consider an example with a LinearLayout
with horizontal orientation and 3 ImageViews
inside it. Now we want these ImageViews
always to take equal space. To acheive this, you can set the layout_weight
of each ImageView
to 1 and the weightSum
will be calculated to be equal to 3 as shown in the comment.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
<!-- android:weightSum="3" -->
android:orientation="horizontal"
android:layout_gravity="center">
<ImageView
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_width="0dp"/>
.....
weightSum
is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.
Related Topics
How to Make Links in a Textview Clickable
Comparison of Android Networking Libraries: Okhttp, Retrofit, and Volley
How to Use Drawerlayout to Display Over the Actionbar/Toolbar and Under the Status Bar
Programmatically Register a Broadcast Receiver
Firebase Data Desc Sorting in Android
Android: How to Get the Current Foreground Activity (From a Service)
Listview Item Background Via Custom Selector
How to Prevent Custom Views from Losing State Across Screen Orientation Changes
How to Get the Source Code from an Apk File
How to Open the Google Play Store Directly from My Android Application
How to Manually Include External Aar Package Using Gradle For Android
Accepting Https Connections With Self-Signed Certificates
Get Visible Items in Recyclerview