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
4
votes
3 answers

Android layout - Two views next to each other, equal width, use full screen-width

I have a problem with a layout in android. I want that two views should have the equal width, and they should almost use the full width of the screen. Each view should contain a centered label. It should look like this when it's done: Here is what…
Mike_NotGuilty
  • 2,253
  • 5
  • 32
  • 64
4
votes
5 answers

Different Layouts with same weights gets different size

In my layout I have two different LinearLayouts. The first one is this:
a.ras2002
  • 385
  • 2
  • 7
  • 21
4
votes
2 answers

How can I give my widgets' width a percentage rather than an explicit value?

Based on how XAML works, I thought I could give my android widgets a percentage value for width. After searching, I found that *theoretically," this is available via the "layout_weight" property. But using this does not produce the desired…
4
votes
2 answers

Invalid layout param in a RelativeLayout: layout_weight

I am getting the below error for my android layout file in Eclipse editor : Invalid layout param in a RelativeLayout: layout_weight Layout :
faizal
  • 3,497
  • 7
  • 37
  • 62
4
votes
1 answer

ListView layout_weight not working, height set to 0dp

I'm trying to use the layout_weight attribute for testing. It works fine excpect for the ListView. I've set the height to 0dp and the weightsum. I've tried to put the ListView into a relative Layout but I get the same result. Here is my code:
4
votes
3 answers

How to set view weights?

I'm trying to get the views inside a LinearLayout to fill the width. I have tried setting it by using LayoutParams, but it gave me an error: My code: EditText et = new EditText(v.getContext()); et.setHint("asset "); …
kyokotsu
  • 145
  • 1
  • 3
  • 14
4
votes
1 answer

Weight specified ListView reduces in size when item is removed

I'm working on a dialog box in android which I intend to make very flexible for different phone-resolutions. Therefore I'm using the layout_weight attribute when building the xml file.
4
votes
2 answers

Creating a grid like layout in Android

Basically, I have a view that needs to be split up into a 2x4 grid, with each section equal in size (i.e. each row would be 25% of the screen height, and each column would be 50% of the screen width). My approach was to use a horizontal LinearLayout…
user1243584
3
votes
1 answer

Fill max width inside FlowRow - jetpack compose

I to want a row where the items goes to the next line if it gets to small. Fair enough, i am using FlowRow for that. I want the items to take up the space on the line they are on. Ok, tried to use weight, but thats not possible. So how can i create…
3
votes
2 answers

When aligned to width LinearLayout layout_weight = “1” requires unit

I use android: layout_weight = "1" to align the elements, but I can't figure out why I need to specify the unit of measurement. What is the reason for this?
bahmN
  • 47
  • 5
3
votes
1 answer

Setting a CardView's height as a custom percentage of the screen size

I have a fragment that displays a RecyclerView that is populated with an Array of objects - 1 CardView for each object. I want my application to display 5 CardView's and have the 6th CardView peeking on the screen regardless of the size (by making…
3
votes
1 answer

How to set layout_weight programmatically?

Disclaimer: AFAIK This question has no answer so far for XAMARIN ANDROID. It has been answered multiple times for Android/Java. My problem is that I have no constructor overload for GridLayoutParams that takes the layout_weight as a parameter... The…
anon
3
votes
1 answer

Wrong height in view added Dynamically to a container in Android: weight, Linear and Constraint Layouts

Introduction I was adding to a Linear Layout the data that I get from an API, so the data is dynamic. It is an app for cryptocurrencies, so for each coin downloaded it draws a new line in a new Layout which is finally added dynamically. The…
manu
  • 187
  • 1
  • 16
3
votes
1 answer

Android - ConstraintVertical weight in ConstraintLayout doesn't work?

I am using from app:layout_constraintVertical_weight="5"inConstraintLayout but it doesn't work.
3
votes
2 answers

adding the views dynamically with layout weight property in Android

I need to add the views in dynamically into my LinearLayout which I already have in my xml file. I tried to add the layouts and able to add it, but the problem is I could not able to set the layout weight property to the custom added views properly.…