• Latest Code...

    Featured Post

    Implementing Hilt in a Kotlin Android Jetpack Compose Project with MVVM Architecture

     In modern Android development, maintaining a scalable codebase can be challenging, especially when it comes to dependency management. Hilt,...

    What is android:layout_weight in Android?


    What happens when i set weight =1 for one layout and weight to 2 for other layout.If i have linear layout, here is the simple solutions:

    The weight is used to distribute the remaining empty space or take away space when the total sum is larger than the LinearLayout.

    Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
    Look more about this information click. Here, and  Link1

    Layout weight problem

    If we are dividing the parent in to equal parts, we just set the children’s layout_weights all to 1. But if we want to divide it unequally, we can do that in a number of ways. We can either use decimal fractional values which total 1, or we can use integer values:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:background="#FF0000"
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:layout_weight="0.66667" />
    <LinearLayout android:background="#00FF00"
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:layout_weight="0.33333" />
    </LinearLayout>
    Or
     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout android:background="#FF0000"
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:layout_weight="2" />
    <LinearLayout android:background="#00FF00"
    android:layout_height="fill_parent" android:layout_width="fill_parent"
    android:layout_weight="1" />
    </LinearLayout>

    Both of these will produce the same result.

    Happy Coding!!!

    Contact Form

    Name

    Email *

    Message *