How to make a layout with fixed sides and expandable center? - android

What do you think would be the best way to make such layout in android?
It should fill the screen's resolution, while having the left and right part fixed 150px in width, but the center part should stretch out as needed.
Thanks!

Best approach will be to use LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<View
android:layout_width="150px"
android:layout_height="fill_parent"
android:layout_weight="0"/>
<View
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<View
android:layout_width="150px"
android:layout_height="fill_parent"
android:layout_weight="0"/>
</LinearLayout>

use RelativeLayout, and take Three Views init:
LayoutParams of EachView should be as:
View1-- id- #+id/view1
Layout_Width= wrap_content
Layout_Height=wrap_content
view2: id- #+id/view2
Layout_Width= fill_parent
Layout_Height=wrap_content
toLeftOF=#+id/view3
toRightOf=#+id/view1
view3: id- #+id/view2
Layout_Width= wrap_content
Layout_Height=wrap_content
align_parent_right=true

http://developer.android.com/reference/android/widget/TableLayout.html - this is probably what you are looking for, specifically http://developer.android.com/reference/android/widget/TableLayout.html#attr_android:stretchColumns

Related

android layout weight and gravity issues

I am trying to get a better understanding of android layouts. In the following example I solved my problem but only through trial and error. I would like to understand WHY it worked.
I am setting up a "header" line inside another layout. I want the first part to use as much of the width as possible and the second part to use only what it needs. So I set layout_1 to have a weight of 1, layout_2 to have a weight of 0. Originally, I had both with layout_width of match_parent. That caused layout_2 to take the entire width and made layout_1 disappear. I finally fixed it by setting width on layout_2 to wrap_content. I understand that it makes sense for layout_2 to have width wrap_content. But don't understand why layout_2 match_parent would take the entire width when it has weight of 0 and layout_1 also has width match_parent.
An example of the code follows.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout android:id="#+id/header_layout_1"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_gravity="center" >
<LinearLayout android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some text"/>
</LinearLayout>
<!-- changing width on header_layout_2 to match_parent takes over layout, wrap_content gives me what I want -->
<LinearLayout android:id="#+id/header_layout_2"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="0">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some more text"/>
</LinearLayout>
</LinearLayout>
<!--end of header-->
</LinearLayout>
You can find lots of sources and blogs on The Internet about LinearLayout and layout_weight.
There are some useful websites:
https://ugia.io/2012/01/19/android-linearlayout-distribution-explained-weight-and-sizes/
http://www.101apps.co.za/index.php/articles/using-android-s-layout-weight-attribute.html
https://blog.stylingandroid.com/layout-weights-part-1/
https://www.mkyong.com/android/android-linearlayout-example/

layout_weight meaning in the case of nested LinerLayout

In this layout definition:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="7"
android:id="topLayout"
android:background="#android:color/holo_green_light">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_weight="2"
android:id="bottomLayout"
android:background="#android:color/holo_orange_light">
</LinearLayout>
</LinearLayout>
I don't understand why the named "bottomLayout" is higher than the topLayout. You can see a commented screenshot of the result in Android Studio.
First of all fix your xml and change layout_height to 0dp.This is because your are using weights to manage height and at the same time your are instructing it to fill parent.
Second, if you'll experiment by giving weights as 1 for each you'll notice that both the layouts are now divided equally.What I assume is that weight is the calculation of available space that's left after adding the view i.e The weight is calculated according to the available space.
Check in your preview by clicking on the outline for any overflowing of layouts out of screen, you might find that some part of your layout is out of screen. To get some clarity either use your weights according to percentage for example instead of giving in 2 and 7 try with 0.2 and 0.8, this will balance the weigts. Or you can use the attribute "weight_sum" to declare total available weight and then distribute it evenly, for example with weight_sum 100 you can follow a percentage based approach.
See this link for further clarity.
LinearLayout children are laid out in order they are declared.
layout_weight mechanism only distributes any remaining space to elements in proportion to their weight, it doesn't affect the ordering.
This is unlike some other environments where a "weight" parameter affects an item's position in a container.
If you make your code to like this then you can find solution
<LinearLayout
android:![enter image description here][1]orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="7"
android:id="topLayout"
android:background="#android:color/holo_green_light">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="3"
android:id="bottomLayout">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="b"
android:id="#+id/button4"
android:layout_gravity="center"
android:background="#android:color/holo_orange_light"/>
</LinearLayout>
if you want to use layout_weight in linearlayout then you have to add weightSum in parent LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical"
>
<--70% of free space in parent LinearLayout-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="7"
>
</LinearLayout>
<--30% of free space in parent LinearLayout-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
>
</LinearLayout>
</LinearLayout>
in xml comments i wrote 70% of free space in parent LinearLayout
if you add some layout with exact height then both your linearlayouts will occupy 70% and 30% of left height in that particular linearlayout
for example if height of your parent linearlayout is 100dp
your child layouts will be drawn first one 70dp and the second one will be 30dp tall
but if you add some imageview with height 50dp then your first child linearlayout will be about 35dp tall and 15dp for second one

make an object in xml that would have the width and height of half of the screen

For the xml line:
android:layout_width="fill_parent", is there a const value like fill_parent/2? I want my object to fill only half of the screen and not all of it.
You can use "weight" of the linear layout for this purpose. e.g. if you want to divide available space between two control:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1.0"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1.0"/>
</LinearLayout>
for proportionate dimension use Layout_weight method for LinearLayout .
for your case
<Linearlayout weight_sum=2>
<yourView layout_weight=1 />
</Linearlayout>
//sudo syntax

How to make a view's size depend on another view's size?

I'm making a UI designed as shown below. View 1 is an image, when showing different pictures, its size will be different. The sizes of view 2 and view 3 are depend on View 1. How to define the xml file to make this happen?
ps: The design came from client so I can't change it.
better use RelativeLayout to achieve this task....Make appropriate changes and use it
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_below="#id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_toRightof="#id/view1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
</RelativeLayout>
I think LinearLayout is better here, because the easiest way to set the appropriate sizes is to use layout_weight. If you set a non-zero weight for only one element in a LinearLayout, then it will take all the remaining space. layout_weight divides the remaining spaces in the ratio of the given weights. It calculates the size of the remaining space after setting the given layout_width-s (or height) first, then it will override these sizes. When you want to divide the whole screen into fixed ratios, not the remaining spaces after some initial setting, then you should first set the size of the elements to 0. So set the size of View1 to wrap_content, and all the other sizes in the appropriate directions to 0 (fill parent in the other direction), and use layout_weight=1.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical" android:layout_width ="wrap_content"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src=.../>
<SomeKindOfView
android:id="#+id/view2"
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1"/>
</LinearLayout>
<AnotherView
android:id="#+id/view3"
android:layout_width="0px"
android:layout_height="fill_parent"
android:layout_weight="1"/>
</LinearLayout>

Set the Layout according to the Height of Screen

I am using two ListView in a screen. I want to divide the height of each ListView but I want it to be screen independent. For this I need to know the Height of the screen. I am using xml to do this.
Can any one please help me to achieve this?
Thank You
Deepak
Try this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
The important parts are having the ListViews android:layout_height="fill_parent" and android:layout_weight="1" and the LinearLayout android:orientation="vertical"
Documentation for more information about the layout_weight attribute.
Try setting the layout height of the views to "fill parent" and layout weight to 1.

Categories

Resources