0

I see great value in fill_parent, and I'd like to make my own implementation of it so I can do more than just fill the remaining space (ex: fill 80% of the remaining space, etc.)

How does fill_parent actually work under the hood? As in how does it compute the space remaining, how does it work at runtime, etc.

I've done similar things to fill_parent in the past where I calculate the space an element should take up based on the current screen size, how much of the screen the element should take up, etc. but I want to know specifically how Android does it with fill_parent.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
Trevor Hart
  • 993
  • 7
  • 26
  • 2
    `fill_parent` has been deprecated in favor of `match_parent`. You may want to look into [Flexbox Layout](https://github.com/google/flexbox-layout). Though I have not used it myself, I think it may offer the functionality you are looking for. – Stephen May 01 '17 at 19:40
  • I'll take a look for sure. My question is though, doesn't match_parent give the element the width of the parent, whereas fill_parent says whatever space is remaining, fill it? – Trevor Hart May 01 '17 at 19:42
  • fill_parent has been renamed match_parent. Otherwise they are the same http://stackoverflow.com/questions/5761960/what-is-the-difference-between-match-parent-and-fill-parent – Rockney May 01 '17 at 19:44
  • Huh..that seems like an odd way to name it, but that works for me, thanks! – Trevor Hart May 01 '17 at 19:45

2 Answers2

6

Try creating a custom View or ViewGroup and you will find out.

There's 3 stages on bringing a View to your screen:

  • measure
  • layout
  • draw

In measure the parent tells the child how much space is available. It may do that in respect to the childs layout parameters. So if the child says match_parent (fill_parent is deprecated) the parent will pass in either its own size, or the remaining space (most of the time...)

The child then takes the available size, calls setMeasuredDimenstion(allTheSpaceIGot) and that's measuring for you.

Next up during layout, the parent checks the childrens measured sizes. It then sets the childrens bounds (top, left, bottom, right) accordingly.

Finally in onDraw every child draws itself within its bounds.


To sum this up:

  • Child gives parent information about its wishes.
  • Parent offers child some available space.
  • Child says "I'll take it".
  • Parent gives child its final restraints
  • Child draws itself within the constraints

If you want to assign say 60% to a view you should have a look at creating a custom ViewGroup (since that is who actually decides on the childs dimensions)

I also wrote a blog post about custom views that goes into more detail, followed by how to create a custom layout.

snachmsm
  • 17,866
  • 3
  • 32
  • 74
David Medenjak
  • 33,993
  • 14
  • 106
  • 134
  • What do you mean "most of the time" it will pass the remaining space? Is there a special case where this isn't what would happen? – Trevor Hart May 01 '17 at 20:27
  • 1
    @TrevorHart Every ViewGroup has their own implementation of how to assign space. FrameLayout, LinearLayout, etc will all adhere to standards, but e.g. say I created that mentioned _60% size layout_. Depending on the actual implementation it could ignore `wrap_content` and `match_parent` and just always give me 60% of the available size. Also e.g ScrollView does things sometimes a bit differently (that's why the view inside a ScrollView should be `wrap_content`, IIRC you get `0` available height with no constraints, thus should measure the full height) – David Medenjak May 01 '17 at 20:31
  • This is all super useful to know, makes sense to me. – Trevor Hart May 01 '17 at 22:11
2

The entire source code for Android is open source, freely available within a few clicks on Google, so you can read it and study it all you want.

But just a fair warning, it's definitely no small task you're trying to accomplish, as there are an enormous amount of cases you have to account for.

If you want a layout to take X percent of available height/width, take a look at PercentageRelativeLayout

Just FYI: 'fill_parent' is deprecated, use 'match_parent' instead. They literally do the exact same thing, it's simply a different word.

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
  • Was not aware of PercentageRelativeLayout, thanks for that, I'll take a look at it. Side note, I know it's a large task, I should say that I just wanted to know generally how it worked and then implement a primitive version of it for my specific use case. – Trevor Hart May 01 '17 at 19:49