HOW TO SET LAYOUT HEIGHT AND WEIGHT IN PERCENTAGE ANDROID?

Pooja Malohtra

Total Post:47

Points:331
Posted by  Pooja Malohtra
 2443  View(s)
Ratings:
Rate this:
How to set layout  height and weight in percentage android
  1. Chris S

    Post:48

    Points:336
    Re: How to set layout height and weight in percentage android?

    You need to add weight in your controls xml file. Here I am giving a sample, which help you to set height and weight in percentage.

       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

         xmlns:tools="http://schemas.android.com/tools"

         android:layout_width="match_parent"

         android:layout_height="match_parent"

         android:orientation="vertical"

       tools:context=".MainActivity" >

       <!--weight=".5" means 50 percantage -->

       <LinearLayout

           android:layout_width="fill_parent"

           android:layout_height="0dp"

           android:layout_weight=".5"

           android:orientation="horizontal" >

           <TextView

               android:layout_width="0dp"

               android:layout_height="wrap_content"

               android:layout_weight=".5"

               android:gravity="center"

               android:text="Sample1" />

            <TextView

                android:layout_width="0dp"

                android:layout_height="wrap_content"

                android:layout_weight=".5"

                android:gravity="center"

                android:text="Sample2" />

        </LinearLayout>

        <LinearLayout

            android:layout_width="fill_parent"

            android:layout_height="0dp"

            android:layout_weight=".5"

            android:orientation="horizontal" >

            <Button

                android:layout_width="0dp"

                android:layout_height="wrap_content"

                android:layout_weight=".5"

                android:gravity="center"

                android:text="Sample1" />

            <Button

                android:layout_width="0dp"

                android:layout_height="wrap_content"

                android:layout_weight=".5"

                android:gravity="center"

                android:text="Sample2" />

        </LinearLayout>

    </LinearLayout>

      

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!