Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Linear/Relative Layout - how to "auto size" object in the middle (3 objects)

I have two cases with Linear (also tried Relative) layout in android. The one happens for horizontal and the other for vertical. Lets start with the horizontal:

it is something like:

<LinearLayout ... >   
    <Button ...  layout:gravity = "left" layout:width = "wrap_content"/>
    <TextView ... layout:width = ??????? />
    <Image  .... layout:gravity = "right" layout:width = "wrap_content"/>
</LinearLayout>

Well , I want the button to stay at the left, the image to stay at the right(stick to the end , not just right of the text view) and the textview to (probably with an autowidth or whatever) to stay in the middle. If I put in textview width = "fill/match_parent it sends the image out of the screen. If I put wrap_content, then the image does not stay at the right of the screen. I have also tried relative layout without success.

Same case in vertical , where I have something like:

<LinearLayout ...>
    <LinearLayout .... layout:height = "wrap_content" layout:gravity= "top" />
    <ListView layout:height = ???????>
    <LinearLayout ... layout:height = "wrap_content" layout:gravity = "bottom" />
</LinearLayout>

Same requirement here. I want the first L.layout to stay on top, List view auto size between them and the 2nd Linear layout to stay at the bottom. (Imagine I'm trying to create a view that looks like a UITableView in iPhone that has a NavigationBar, the list of items and a ToolBar at the bottom. Fist LinearLayout is the NavigationBar, the LIst view are the cells and the second LinearLayout is the ToolBar).

Any suggestions? Would prefer the xml solutions.

like image 771
Panos Avatar asked Aug 03 '12 06:08

Panos


People also ask

How do I place a view at center of relative layout?

Below that, the layout_height=0 and layout_weight=1 attributes on the RelativeLayout cause it to take up all the remaining space. You can then center the button in the RelativeLayout . You can play with padding on the button to get it to the size you want.

How do you align elements in linear layout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.

How do you add weight in relative layout?

RelativeLayouts do not support weight. You need to use a LinearLayout as a parent container if you want to use weights.


1 Answers

It can simply done by using RelativeLayout here we go.

Horizontal alignment

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">  

  <Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="something"
    />

  <TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/image"
    android:layout_toRightOf="@+id/button" />

  <ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:background="@drawable/icon" />
</RelativeLayout>

Vertical alignment

  <LinearLayout
    android:id="@+id/linear_top"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

  <ListView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/linear_top"
    android:layout_above="@+id/linear_bottom" />

  <LinearLayout
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />
</RelativeLayout>
like image 176
Mohammed Azharuddin Shaikh Avatar answered Sep 18 '22 18:09

Mohammed Azharuddin Shaikh