android:layout_height 50% of the screen size

AndroidAndroid LayoutSizeAndroid LinearlayoutAndroid Layout-Weight

Android Problem Overview


I just implemented a ListView inside a LinearLayout, but I need to define the height of the LinearLayout (it has to be 50% of the screen height).

<LinearLayout
	android:id="@+id/widget34"
	android:layout_width="300px"
	android:layout_height="235px"
	android:orientation="vertical"
	android:layout_below="@+id/tv_scanning_for"
	android:layout_centerHorizontal="true">
	
	<ListView
		android:id="@+id/lv_events"
		android:textSize="18sp"			
		android:cacheColorHint="#00000000"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:layout_below="@+id/tv_scanning_for"
		android:layout_centerHorizontal="true">
	</ListView>
	
</LinearLayout>

Is that possible?

I did something similar for a button and an EditText, but doesn't seem to work on Layouts.

This is my Code:

    //capture the size of the devices screen
    Display display = getWindowManager().getDefaultDisplay();
    double width = display.getWidth();
    
    //my EditText will be smaller than full screen (80%)        
    double doubleSize = (width/5)*4;
    int editTextSize = (int) doubleSize;
    
    //define the EditText 
    userName = (EditText) this.findViewById(R.id.userName);
    password = (EditText) this.findViewById(R.id.password);
    
    //set the size
    userName.setWidth(editTextSize);
    password.setWidth(editTextSize);

Android Solutions


Solution 1 - Android

Set its layout_height="0dp"*, add a blank View beneath it (or blank ImageView or just a FrameLayout) with a layout_height also equal to 0dp, and set both Views to have a layout_weight="1"

This will stretch each View equally as it fills the screen. Since both have the same weight, each will take 50% of the screen.

*See adamp's comment for why that works and other really helpful tidbits.

Solution 2 - Android

This is easy to do in xml. Set your top container to be a LinearLayout and set the orientation attribute as you wish. Then inside of that place two linearlayouts that both have "fill parent" on width and height. Finally, set the weigth attribute of those two linearlayouts to 1.

Solution 3 - Android

This is my android:layout_height=50% activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/alipay_login"
        style="@style/loginType"
        android:background="#27b" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/taobao_login"
        style="@style/loginType"
        android:background="#ed6d00" >
    </LinearLayout>

</LinearLayout>

style:

<style name="loginType">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:layout_weight">0.5</item>
    <item name="android:orientation">vertical</item>
</style>

Solution 4 - Android

best way is use

layout_height="0dp" layout_weight="0.5"

for example

<WebView
	android:id="@+id/wvHelp"
	android:layout_width="match_parent"
	android:layout_height="0dp"
	android:layout_weight="0.5" />

<TextView
	android:id="@+id/txtTEMP"
	android:layout_width="match_parent"
	android:layout_height="0dp"
	android:layout_weight="0.5"
	android:text="TextView" />

WebView,TextView have 50% of the screen height

Solution 5 - Android

To make sure the height of a view is 50% of the screen then we can create two sub LinearLayouts in a LinearLayout. Each of the child LinearLayout should have "android:layout_weight" of 0.5 to cover half the screen

the parent LinearLAyout should have "android:orientation" set to vertical

.

.

here is code for your reference.... this code contains two buttons of height half the screen

<LinearLayout 
android:orientation="vertical"
android:layout_height="match_parent">

<LinearLayout

    android:layout_weight="0.5"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:padding="10dp"
        android:layout_weight="0.5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="button1"
        android:id="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        />

    <Button
        android:padding="10dp"
        android:layout_weight="0.5"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="button2"
        android:id="@+id/button2"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        />

    </LinearLayout>

<LinearLayout

    android:layout_weight="0.5"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    </LinearLayout>
   </LinearLayout>

Solution 6 - Android

This kind of worked for me. Though FAB doesn't float independently, but now it isn't getting pushed down.

Observe the weights given inside the LinearLayout

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/andsanddkasd">

            <android.support.v7.widget.RecyclerView
                android:id="@+id/sharedResourcesRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="4"
                />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="bottom|right"
                android:src="@android:drawable/ic_input_add"
                android:layout_weight="1"/>

        </LinearLayout>

Hope this helps :)

Solution 7 - Android

You should do something like that:

<LinearLayout
    android:id="@+id/widget34"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_below="@+id/tv_scanning_for"
    android:layout_centerHorizontal="true">

    <ListView
        android:id="@+id/lv_events"
        android:textSize="18sp"         
        android:cacheColorHint="#00000000"
	    android:layout_height="1"
        android:layout_width="fill_parent"
	    android:layout_weight="0dp"
        android:layout_below="@+id/tv_scanning_for"
        android:layout_centerHorizontal="true"
        />

</LinearLayout>

Also use dp instead px or read about it here.

Solution 8 - Android

it's so easy if you want divide your screen two part vertically ( top30% + bottom70%)

<LinearLayout
            android:id="@+id/LinearLayoutTop"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2">
           
     </LinearLayout>
     <LinearLayout
            android:id="@+id/LinearLayoutBottom"
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1">
           
     </LinearLayout>
       

Solution 9 - Android

To achieve this feat, define a outer linear layout with a weightSum={amount of weight to distribute}.

it defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.Another example would be set weightSum=2, and if the two children set layout_weight=1 then each would get 50% of the available space.

WeightSum is dependent on the amount of children in the parent layout.

Solution 10 - Android

You can use android:weightSum="2" on the parent layout combined with android:layout_height="1" on the child layout.

           <LinearLayout
                android:layout_height="match_parent"
                android:layout_width="wrap_content"
                android:weightSum="2"
                >

                <ImageView
                    android:layout_height="1"
                    android:layout_width="wrap_content" />

            </LinearLayout>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMataMixView Question on Stackoverflow
Solution 1 - AndroidLeffelManiaView Answer on Stackoverflow
Solution 2 - AndroidEmir KuljaninView Answer on Stackoverflow
Solution 3 - AndroidPraise SongView Answer on Stackoverflow
Solution 4 - AndroidMohammad FooladyView Answer on Stackoverflow
Solution 5 - AndroidswayamrainaView Answer on Stackoverflow
Solution 6 - AndroidMagsView Answer on Stackoverflow
Solution 7 - AndroidnenitoView Answer on Stackoverflow
Solution 8 - Androidahmad zeiniView Answer on Stackoverflow
Solution 9 - AndroidRemarioView Answer on Stackoverflow
Solution 10 - Androidpatrick-fitzgeraldView Answer on Stackoverflow