Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Layout, view height is equal to screen size

in Android how to make a view have same height as its screen size, is it possible to achieve this with only the xml? or if it must use script, tell me how

Thanks.


Sorry for being not clear, and thanks for your reply

but i think, match_parent and fill_parent attribute is not reliable, because when i put the view inside one container or change the view container hierarchy, it won't work.

Here my complete xml layout.

The element i want to make the height sam with device screen is the last list view inside relative layout

enter image description here

like image 475
DeckyFx Avatar asked Nov 06 '13 07:11

DeckyFx


People also ask

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's the size of my screen?

The size of a desktop computer monitor is determined by physically measuring the screen. Using a measuring tape, start at the top-left corner and pull it diagonally to the bottom-right corner. Be sure to only measure the screen; do not include the bezel (the plastic edge) around the screen.


1 Answers

No you cannot achieve this in XML only.

As Android supports multiple screen sizes, at runtime you need to check for each device size. The height for each device can be calculated like this:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;

With the above code, you will get the height of the screen and you need to set this height in dp to your view at runtime.

Do this in your activity:

// get view you want to resize
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main); 

// get layout parameters for that view
ViewGroup.LayoutParams params = mainLayout.getLayoutParams();

// change height of the params e.g. 480dp
params.height = 480;

// initialize new parameters for my element
mainLayout.setLayoutParams(new LinearLayout.LayoutParams(params));
like image 67
Jitender Dev Avatar answered Oct 04 '22 07:10

Jitender Dev