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
9
votes
1 answer

How to refresh match_parent in a linearLayout after using setRotation?

I have several nested layouts that I'm trying to rotate 90 degrees on demand in code. I've got the setRotation part of things working just fine, but unfortunately things aren't resizing quite right with the rotation. The width on these elements is…
7
votes
1 answer

How to handle weights in onMeasue for custom views?

I have a custom view which I'm creating that will scale the font size of its child TextViews to fit all in a set width, so each is on its own line. Obviously it's going to need the width to figure that out. I had overridden onMeasure() like…
7
votes
1 answer

How can I resize two fragments in the screen by focus?

I have three fragments, the first two filling 80% of the screen and the last one with the rest (this one is never going to change in size). I want to, after input from the user (focus) in a fragment, resize the fragment so it fills 70% of the screen…
Gardener
  • 177
  • 8
7
votes
2 answers

Android - How can I set an item in a Toolbar to fill all of the available space?

I've tried assigning weights of course, but it appears that isn't supported by Toolbar.
Noah Andrews
  • 353
  • 2
  • 14
7
votes
2 answers

Android: Setting the Weight parameter programmatically does the opposite of what i want

I'm having a problem with android. I'm setting the Weight parameter in Java, but it's doing exactly the oposite of what I want. Here's the code LinearLayout container = new LinearLayout(context); // some code ... container.setWeightSum(1f); View v1…
Firas
  • 562
  • 6
  • 19
6
votes
3 answers

Issue with defining weight to views in Jetpack compose

I want to make a simple jetpack compose layout, using weights. below is the code Row() { Column( Modifier.weight(1f).background(Blue)){ Text(text = "Weight = 1", color = Color.White) } Column( Modifier.weight(2f).background(Yellow) ) { …
6
votes
1 answer

Corrispective of android:weight in JavaFX

I'm developing a small software for training in JavaFX. In Android, inside a LinearLayout, I can expand a view to fill the whole space available by setting the variable android:weight="someValue", unfortunately I'm unable to achieve this is…
Lampione
  • 1,622
  • 3
  • 21
  • 39
6
votes
4 answers

match_parent is not working in custom toolbar

I have made custom toolbar with search and setting icon . when i click on search icon edittext should apper with match_parent and should occupy available space till the search icon . However it is not happening . it just appear with very small space…
6
votes
1 answer

if I set linearlayout weight when keyboard pops it resizes its continent

I folowed this example and manage to put fab button in right place but when I press on edit text keyboard pops up and it messes up layout weight How can I make that ImageView would be static and when keyboard opens it would stay on same…
6
votes
2 answers

Android layout: place a view between two views

I am trying to make a screen, preferably with XML only, to look like this: I followed this approach, but using FrameLayout I only have the option to position the "orange" view using layout_gravity, and as I try to explain in the image, I don't know…
RominaV
  • 3,335
  • 1
  • 29
  • 59
6
votes
3 answers

Flexible Horizontal Layout with `layout_weight` and `maxWidth`

I have a TextView and a square ImageView that I would like to show in a horizontal linear layout. Each should take half of the parent view's width and the height should fit the content (i.e. the image). The text should be centered…
user3386180
6
votes
2 answers

How to align elements horizontally within a LinearLayout?

I am not able to control the height of the LinearLayout. These won't align properly and won't fill up the width. I want the divider to be in the center and both the buttons on either side. Here is my code:
Prince
  • 20,353
  • 6
  • 39
  • 59
6
votes
2 answers

How to divide layout to 3 parts?

I'm trying to divide my layout to 3 equal rows, trying to use LinearLayout and weights, but it doesn't work, it doesn't make any change:
Gintas_
  • 4,940
  • 12
  • 44
  • 87
6
votes
1 answer

Android View: hide if layout_height too small

I want to show just two TextViews on the screen, one on top of the other. TextView B: 400dp high, at the bottom, TextView A: fill the rest of the screen. However, if the height of TextView A is less than 100dp, it shouldn't be displayed (only…
6
votes
1 answer

Android How to setWeight of a RemoteView?

I need to know the code that allow me to set the Weight of a RemoteView. I tried with this code but this doesn't work: RemoteViews remoteViews = new RemoteViews(c.getPackageName(), R.layout.notification); remoteViews.setInt(R.id.ll_notification,…
Meroelyth
  • 5,310
  • 9
  • 42
  • 52
1
2
3
30 31