Android layout weight problem - android

friends,
i have written following layout code and buttons to be displayed on screen equally but it not seems to work any one guide me what mistake am i doing?
![<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#color/color_panel_background"
>
<ImageButton
android:layout_height="wrap_content"
android:id="#+id/currentLocation"
android:layout_weight="1"
android:layout_width="fill_parent"
android:src="#drawable/current_location_icon"
/>
<ImageButton
android:layout_height="wrap_content"
android:id="#+id/searchCity"
android:layout_weight="1"
android:layout_width="fill_parent"
android:src="#drawable/search_icon"
/>
<ImageButton
android:layout_height="wrap_content"
android:id="#+id/home"
android:layout_weight="1"
android:layout_width="fill_parent"
android:src="#drawable/home_icon"
/>
</LinearLayout>
</RelativeLayout>][2]

The drawable for the middle button is obviously bigger than the other two. You set the height of the buttons with wrap_content, so the middle button gets bigger. Weight has nothing to do with this. Its only defining how much space the item takes when you use fill_parent.
Easiest way to fix this is either changing the layout_height to a fixed value (use dp as unit)
or change the size of your drawables making the images all the same size to begin with.
Thumbs up for the way you asked the question. Screenshot and relevant code. Wish everybody would do that :)

Change android:layout_height from "wrap_content" to some constant if you want them to have equal height.

change your linear layout height to some constant and
change object layout_height within the linear layout to fill_parent

in Horizontal: if you set weight for child view then set with = 0dp
in Vertical: if you set weight for child view then set height = 0dp

Related

How can I ignore a child element's margins?

Background
I'm developing a custom notification layout by injecting an OS-generated notification view into my own layout. My layout must be as short as possible, which is 50dp in Android 10.
Problem
The view that I'm injecting into my view has margins that cause it to stretch my layout from 50dp to 66dp.
Code
The following layout is a simplification of what's going on to demonstrate the problem:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="#+id/activity_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="horizontal"
>
<TextView
android:id="#+id/full_height_view"
android:layout_width="10dp"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright"
/>
<TextView
android:id="#+id/bad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="46dp"
android:layout_marginBottom="20dp"
android:background="#android:color/holo_red_light"
>
</TextView>
</LinearLayout>
</FrameLayout>
Note that container has minHeight of 50dp, which I don't want to be exceeded. The problem is the margins from bad_view sum up to 66dp and stretch the parent to 66dp.
Question
How can I prevent the margins on bad_view from stretching the parent beyond its minimum height? I cannot set a fixed height on the parent because the exact height is OS-dependent. And I cannot modify bad_view because it's generated by the OS.
I ended up solving this by setting the visibility of bad_view to gone.
I found another solution: use GridLayout vertical weights to override the problematic view's height. This gave me the flexibility of a weighted horizontal LinearLayout but with the addition of vertical weights.

Making a View occupy a fixed width in Android layout

I have this as part of my layout:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="0.15">
<TextView
android:id="#+id/question_text"
android:layout_width="0dip"
android:layout_height="fill_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/score_label" />
<TextView
android:id="#+id/score_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" />
</LinearLayout>
The first TextView is empty at the beginning of the application. Its content is changed dynamically. This makes it occupy zero space so that the second TextView is aligned to the left, even though its layout_gravity is set to right.
How can I make it occupy a fixed width, without taking the contents into account?
I thought about using layout_weight, but I know the recommendation is against using nested weights (the parent ViewGroup has a layout_weight attribute). Maybe I should use a RelativeLayout?
Thanks for any suggestions.
I solved a similar problem using the attribute android:ems="<some number>" on the TextView. An "ems" is the width of the character "M". This attribute makes the TextView exactly the given no. of ems wide.
http://developer.android.com/reference/android/widget/TextView.html
You have all of your TextViews width set to android:layout_width="wrap_content" which means that if there's nothing in there it will have no width. You need to set that to either "match_parent" which will make it the same width as it's parent container or set it to a fixed value, something like android:layout_width="100dp".

Android: Bottom nav bar with equally space buttons

Hi I'm trying to create a bottom nav bar for my application similiar to the iphone zappos app:
I currently have a linear layout within my relative layout. The code is here:
<LinearLayout
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="60dip"
android:orientation="horizontal">
<Button
android:layout_height="fill_parent"
android:id="#+id/navhomebtn"
android:text="Home"
android:layout_width="wrap_content" />
<Button
android:layout_height="fill_parent"
android:id="#+id/navsearchbtn"
android:text="Search"
android:layout_width="wrap_content" />
<Button
android:layout_height="fill_parent"
android:id="#+id/navfavbtn"
android:text="Favorites"
android:layout_width="wrap_content" />
<Button
android:layout_height="fill_parent"
android:id="#+id/navloanbtn"
android:text="Loans"
android:layout_width="wrap_content" />
</LinearLayout>
When I change the buttons to fill parent they just overlap each other and don't respect the others size. I was hoping to get them to fill the space so different size phones will all have a similar UI experience. Otherwise I would have to set them as fixed width and have it centered with a background that blends (which I don't want to do).
Please help!
Use layout_weight of 1 in your LinearLayout and use 0dp for layout_width and 0.25 for layout_weight in your buttons.
Use layout_weight on the buttons. Set all button's layout_weight to 1 and each will take 1/n of its parent's width.
Try passing 0dip for each layout_width.
As the other guys mentioned, the easiest way is to set the layout_width to 0dip and set the layout_weight. An alternate approach would be to use a RelativeLayout instead, and manually set the layout_alignParentLeft/Right and layout_toLeft/Rightof.

In android layouts what is the effect/meaning of layout_height="0dip"

I've seen several examples that use
android:layout_height="0px" or "0dip" but i do not understand the impact of this. It seems that would make the layout 0 pixels tall. Is the value mitigated but some other factor like 'weight' or the height of any parent views?
Yep you are right about the weight, when you want the width or height to be controlled by weight its convention to set that value to 0dip and let the weight control the actual value. Although I am pretty sure 0 is just arbitrary here you could put anything but putting 0 makes your intention more clear.
When using a LinearLayout if you set the layout_weight to a non-zero value and set the layout_height (or layout_width) to 0px or 0dip then the LinearLayout distributes any unassigned space along the appropriate axis based on the weights. So for example, if you look at the layout below the View with id *gestures_overlay* it has layout_height 0dip and layout_weight 1 so the parent LinearLayout stretches it to fill the available vertical space between the 2 surrounding LinearLayouts. If there was another View with the same 0dip layout_height and a layout_weight value then they would share the vertical space based on their weight values.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dip"
android:text="#string/prompt_gesture_name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/gesture_name"
android:layout_width="0dip"
android:layout_weight="1.0"
android:layout_height="wrap_content"
android:maxLength="40"
android:singleLine="true" />
</LinearLayout>
<android.gesture.GestureOverlayView
android:id="#+id/gestures_overlay"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:gestureStrokeType="multiple" />
<LinearLayout
style="#android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/done"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false"
android:onClick="addGesture"
android:text="#string/button_done" />
<Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="cancelGesture"
android:text="#string/button_discard" />
</LinearLayout>
</LinearLayout>
A relevant example from the official developer docs (http://developer.android.com/guide/topics/ui/layout/linear.html):
Equally weighted children
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1".
When we have to assign equal weight to the different view in LINEAR LAYOUT
then we assign either layout/width = 0dp(for horizontal orientation) or layout/height = 0dp(for vertical orientation) and set View /weight ==1 for every view inside that Linear Layout.
Advantage::::
-- on assigning width or height to 0dp then it have no impact and due to weight==1 all the view occupies same space and covered the whole screen size.

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>

Categories

Resources