Questions tagged [android-layout-weight]

In Android by setting a layout_weight for the Views inside the LinearLayout, the parent layout divides the available space between its children according to their weights.

Description

Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams.

By setting a layout_weight for the Views inside the LinearLayout, the parent layout divides the available space between its children according to their weights.

Implementation

A Vertical LinearLayout with 2 Views. Top View has layout_weight of 3 and bottom one has 1, which means total of 4. Top View will fill 3/4 of the Layout and bottom one will fill 1/4 of the Layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <View
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="3"/>

    <View
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1"/>    

</LinearLayout>

Link

Android Developers

451 questions
301
votes
13 answers

How to set layout_weight attribute dynamically from code?

How can I set the value for the attribute layout_weight for button in android dynamically from java code ?
Adham
  • 63,550
  • 98
  • 229
  • 344
287
votes
19 answers

Linear Layout and weight in Android

I always read about this funny weight value in the Android documentations. Now I want to try it for the first time but it isn't working at all. As I understand it from the documentations this layout:
Janusz
  • 187,060
  • 113
  • 301
  • 369
223
votes
12 answers

Set the layout weight of a TextView programmatically

I'm trying to dynamically create TableRow objects and add them to a TableLayout. The TableRow objects has 2 items, a TextView and a CheckBox. The TextView items need to have their layout weight set to 1 to push the CheckBox items to the far right. I…
eugene
  • 2,519
  • 3
  • 17
  • 5
175
votes
10 answers

What is android:weightSum in android, and how does it work?

I want to know: What is android:weightSum and layout weight, and how do they work?
Dhiral Pandya
  • 10,311
  • 4
  • 47
  • 47
52
votes
10 answers

android:layout_height 50% of the screen size

I just implemented a ListView inside a LinearLayout, but I need to define the height of the LinearLayout (it has to be 50% of the screen height).
44
votes
9 answers

Android how to display 2 listviews in one activity one after the other

I have used this code to display 2 list view one on top of the other.
Shivam Bhalla
  • 1,879
  • 5
  • 35
  • 63
36
votes
5 answers

How do I set android:layout_columnWeight="1" programmatically to an element in an android support v7 Gridlayout

I'm trying to build a GirdLayout programmatically with 2 columns and I want these columns to have equal width set at half the width of the screen. I figured out you can do this since API 21 or with the support v7 GirdLayout view. I see examples…
hans_dc
  • 529
  • 1
  • 5
  • 15
32
votes
4 answers

Android Linear Layout Weight Programmatically

I want to add three linear layouts to an activity programatically each of same width. the problem is i am not able to set the weights of these layouts programmatically. I could do this within xml, but I want to do this in program. here is what I…
19
votes
2 answers

Android: trying to understand android:layout_weight

I'm trying to divide a page in three parts. I'd like to do it in percentage values, however that is not supported by Android. Instead I have to use android:layout_weight. But I have a hard time understanding it and getting it right. Especially how…
znq
  • 44,613
  • 41
  • 116
  • 144
18
votes
4 answers

Replacement for the linearLayout weights mechanism

Background: Google suggests to avoid using nested weighted linearLayouts because of performance. using nested weighted linearLayout is awful to read, write and maintain. there is still no good alternative for putting views that are % of the…
11
votes
5 answers

how to set textview width wrap_content but limit to 1/3 of parent's width

Having a TextView, its width should not go over 1/3 of its parent's width. If its width is smaller than 1/3 of parent, it should have wrap_content behavior. Its horizontal sibling will always start next to it. Tried following, it always has hard cut…
lannyf
  • 9,865
  • 12
  • 70
  • 152
11
votes
3 answers

How to set weight for included layouts?

I have two xml files, header and footer as shown below header.xml
svs
  • 377
  • 9
  • 21
10
votes
2 answers

layout_weight working opposite of what it should

In the following xml, I'm trying to draw a layout that contains two blocks (a LinearLayout and a TextView). I want the LinearLayout to be 5 times larger than the TextView. This xml produces the exact opposite of what I expected, the TextView takes 5…
TrtG
  • 2,778
  • 6
  • 26
  • 39
10
votes
1 answer

TableLayout weight sum issues

I'm trying to implement a 40/60 percent split in my TableLayout but I'm having no luck. When testing the code on my Android Device the left TextView still takes up what looks like 60%, instead of 40%.
9
votes
4 answers

Android set max width in percentage of a TextView in LinearLayout of vertical orientation

I'd like to set the layout_weight of the TextView with the tv_long_text to 80% in the following LinearLayout of vertical orientation.
s-hunter
  • 24,172
  • 16
  • 88
  • 130
1
2 3
30 31