Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android:layout_height 50% of the screen size

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); 
like image 463
MataMix Avatar asked Jul 22 '11 17:07

MataMix


People also ask

How do I get the screen size on my Android?

1. Display display = getWindowManager(). getDefaultDisplay(); Point size = new Point(); display. getSize(size); int width = size.

What is layout width and height in android?

layout_width : the width, either an exact value, WRAP_CONTENT , or FILL_PARENT (replaced by MATCH_PARENT in API Level 8) layout_height : the height, either an exact value, WRAP_CONTENT , or FILL_PARENT (replaced by MATCH_PARENT in API Level 8) Parameters. c. Context : the application environment.

What does Android layout weight do?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.


2 Answers

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.

like image 92
LeffelMania Avatar answered Oct 05 '22 06:10

LeffelMania


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.

like image 26
Emir Kuljanin Avatar answered Oct 05 '22 06:10

Emir Kuljanin