LinearLayout with gravity and weights - android

In my LinearLayout the use of weight in Combination with Gravity causes to reverse the effect of weight. The Layout:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0.2">
<TextView android:id="#+id/feedfragmentTitle" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_weight="8"/>
<TextView android:id="#+id/feedfragmentDate" android:layout_height="wrap_content" android:gravity="right" android:layout_width="fill_parent" android:layout_weight="2"/>
</LinearLayout>
The given LinearLayout contains a Title, which should appromately take 80% of the Layout and an Date+Time with 20%. The Date+Time should have the Gravity of right. The layout i posted doesnt work unfortunately and if i play with the parameters weight, the result is the inversed one.
Is there another way to get the Results without swapping the weights ?

When you use the layout_weight property you need to make the appropriate height or width as 0dip
try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/feedfragmentTitle"
android:layout_height="wrap_content" android:layout_width="0dip"
android:layout_weight="8" android:text="sdfdfsfsd"/>
<TextView android:text="aass" android:id="#+id/feedfragmentDate"
android:layout_height="wrap_content"
android:gravity="right" android:layout_width="0dip"
android:layout_weight="2"/>
</LinearLayout>

Try like this
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/feedfragmentTitle" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="left"/>
<TextView android:id="#+id/feedfragmentDate" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_width="wrap_content" />
</LinearLayout>

Related

Need xml layout for the below screen

I am working on creating a page with a layout with header "Account Information" (ref.. image). Followed by a table and i need to populate the table dynamically after fetching from server but not able to do so. Below is the .xml file and attached is the image:
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#layout/header_gradient" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_weight="0.20"
android:gravity="center_horizontal"
android:padding="15dp"
android:text="#string/my_account_header_text"
android:textColor="#ffffff"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
<RelativeLayout
android:id="#+id/my_activity_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="#+id/my_acc_component"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:text="#string/my_account_component"
android:textColor="#372c24"
android:textSize="#dimen/login_credentials_txt" />
<TextView
android:id="#+id/my_acc_details"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/my_acc_component"
android:text="#string/my_account_details"
android:textColor="#372c24"
android:textSize="#dimen/login_credentials_txt" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
This may not be your issue, since I don't know what the problem is yet, but there is an issue that I see. When using weight in a LinearLayout with a vertical orientation your height should be 0dp. Similarly, if it is a horizontal orientation then width should be 0dp. So for example you would change your layouts to
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff" >
and
<RelativeLayout
android:id="#+id/my_activity_layout"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff" >
Also, this is definitely not whatever your problem is but fill_parent is deprecated and match_parent should be used instead.
*Actually, there is a problem with the weight property... if you are working with weight then you better use weight_sum property in the LinearLayout and give your textView or other view an percentage depending on your giver weight_sum value in the parent layout...
However,I think what you are looking for is something like this:*
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center|top"
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/header"
android:background="#drawable/image" // if you want your title/header to be upon the image. Or just create another ImageView for that image
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="100"
android:orientation="horizontal"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:margin_Top="50dp"
android:weight="50"
android:id="#+id/my_acc_component"
android:text="your text"
android:textSize="your Size"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:margin_Top="50dp"
android:weight="50"
android:id="#+id/my_acc_details"
android:text="your text"
android:textSize="your Size"
/>
</LinearLayout>
</RelativeLayout>

Forcing HorizontalScrollView to take up full width and center children

I want a horizontal scrollview to take up the full width of the screen and have its child view centered. So far, I'm ending up with it just wrapping the children and everything is left aligned. My view looks like this:
<HorizontalScrollView
android:id="#+id/mainMenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#CD000000"
android:fillViewport="true"
android:gravity="center_horizontal"
android:scrollbars="none" >
</HorizontalScrollView>
Inside that we inflate something that looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/mode_panzoom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:minWidth="75dip"
android:gravity="center_horizontal">
<ImageView
android:src="#drawable/ic_pan"
android:layout_width="25dip"
android:layout_height="wrap_content"
android:text="#string/mode_panzoom"
android:scaleType="centerInside"/>
</LinearLayout>
<LinearLayout
android:id="#+id/mode_annotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:minWidth="75dip"
android:gravity="center_horizontal">
<ImageView
android:src="#drawable/ic_annotate"
android:layout_width="25dip"
android:layout_height="wrap_content"
android:text="#string/mode_panzoom"/>
</LinearLayout>
<LinearLayout
android:id="#+id/mode_measure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:minWidth="75dip"
android:gravity="center_horizontal">
<ImageView
android:src="#drawable/ic_measure"
android:layout_width="25dip"
android:layout_height="wrap_content"
android:text="#string/mode_panzoom"
android:gravity="center"/>
</LinearLayout>
fillviewport seems to do nothing, and applying gravities seeems to have no effect, probably because its wrapping content even though I set fill_parent as the width.
I figured out my problem, and it ended up being nothing related to the xml. It was the fact that I was inflating the contents of the HorizontalScrollView. After that, if you want your parent to fill_parent correctly you need to force it to layout again via request_layout. Otherwise it won't get the correct size.

Layout views with dynamic width

I have a layout with two children one after another, like this:
|<view1><text1> |
<view1> may change it's width. So, I want it increases while both views stay on the screen:
|<really_wide_view1><text1> |
But when there is no more space at right, it stops:
|<reeeeeeeeeeeally_wide_vi...><text1>|
Is there easy way to do this?
Now I try to use this layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF00aa00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loooooooooooooooooooooooooooooooooooong text" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#FFaa0000"
android:minWidth="30dp"
android:singleLine="true"
android:text="test" />
</LinearLayout>
But result is the same:
I am able to solve your problem using the following code (a modified version of yours):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FF00aa00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:background="#FFaa0000"
android:text="testtest" />
</LinearLayout>
Here is a screenshot of the same:
Let me know if the above piece of code solves the problem.
Try to give both views inside the RelativeLayout and make the views layout width and as a wrap_content and give orientation as horizontal. I think it will solve your problem.
Here is solution with TableLayout, but it looks dirty.
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="0" >
<TableRow>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
</TableLayout>
It works for any length of both text views.

How would I go about making a header in an Android Layout

The header I'm looking for would look something like this:
Just need something to stay to the left, stay in the center, and stay to the right. Everything I have tried has had the center moved over a little towards the right so it looks off center. Any ideas or suggesstions?
EDIT:
This seems to be the closest I've been able to do with a simple amount of code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="First"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Second"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="3"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
Thanks in advance
I'll tell you how to do this. Set up a horizontal LinearLayout with layout_width of fill_parent and layout_height of wrap_content and weightSum of, in your case, 3. Fill it with three TextViews with layout_width set to fill_parent, layout_height set to wrap_content and layout_weight set to 1. Then the first TextView's gravity to "left", second one's to "center" and the third one's to "right". Done.

Layout problems. Android

I have some behaviour of my layout. I give two of them, very similar to each other. Only one difference is the width attribute of the textviews.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:isScrollContainer="true"
android:gravity="center"
android:background="#color/black">
<LinearLayout
android:id="#+id/tabs"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#color/black"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Einstellungen"
android:drawableTop="#drawable/selector_settings"
android:id="#+id/main_menu_button_connection"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10px">
</TextView>
...
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Suche"
android:drawableTop="#drawable/selector_search"
android:id="#+id/main_menu_button_connection"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10px">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
If layout_width is set to wrap content, I get following layout results:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:isScrollContainer="true"
android:gravity="center"
android:background="#color/black">
<LinearLayout
android:id="#+id/tabs"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="#color/black"
android:layout_weight="1">
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="Einstellungen"
android:drawableTop="#drawable/selector_settings"
android:id="#+id/main_menu_button_connection"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10px">
</TextView>
...
<TextView
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="Suche"
android:drawableTop="#drawable/selector_search"
android:id="#+id/main_menu_button_connection"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="10px">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
If layout_width is set to a value instead (here 70dp), my layout looks so, I want it to have:
Can anybody explain me, why?!
Is there some solution avoiding to set a constant width and still having layout result like at the second picture?!
Thank you in advance
You probably want to give each TextView an equal layout weight such as android:layout_weight="1" and a width of fill_parent. Also, don't give a weight for the tabs linear layout. Also, while I don't really know what's going on later in the layout file, having three nested LinearLayouts seems like it might be wasteful. Maybe you can get rid of one of them?
I recommend starting over and using the eclipse designer view or use droiddraw to create the layout, especially since there are recent improvements to the layout editor.

Categories

Resources