Marc DiNino
Marc DiNino

Reputation: 812

What is the best way to have part of a layout fill the remaining space?

I'm sure this question has been asked in some form or another but I can't find anything that gives me what I am looking for.

I am making an Android layout that looks something like the example below. The entire screen is a vertical layout. The area in gray shows a webview The view in orange contains a single button and some padding information.

I want to get Android to "figure out" how big the gray area should be with only knowledge of the button and padding size. Is there any good way to do this?

Note: I usually accomplish it with layout weights, but I'm curious if there is a better way.

example

Upvotes: 0

Views: 51

Answers (2)

user7951234
user7951234

Reputation:

In Relative layout first layout to view the webview .The orange portion above to set the webview layout and Orange Button to set

android:alignparentBottom="true"

Upvotes: 0

David Medenjak
David Medenjak

Reputation: 34522

I usually accomplish it with layout weights, but I'm curious if there is a better way.

No.

LinearLayout with weights, RelativLayout, or ContraintLayout, it all comes down to the same thing: You have some constraint that needs to be resolved. Your layout is simple enought, so just pick whichever method you prefer, since there is not really a performance difference or impact.


In case you need that performance (say it's the top level container of a really complex layout) the best thing you could do is just create a custom ViewGroup that measures the view at the bottom before assigning the leftover height to your other view.

That would be the "clean" solution, since you know about the constraints and can just implement them straight ahead.

Upvotes: 1

Related Questions