2

I'm using the PercentFrameLayout as an item in RecyclerView below is my layout. PercentFrameLayout is not appearing. It's not showing up.

Anything I'm missing here..?

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/header_image_view"
        android:layout_width="match_parent"
        app:layout_heightPercent="12%"
        android:background="@drawable/placeholder_image"/>
</android.support.percent.PercentFrameLayout>
Floern
  • 33,559
  • 24
  • 104
  • 119
Amjed Baig
  • 410
  • 5
  • 9

1 Answers1

8

When you use layout_heightPercent, you are asking for a percentage of the space allotted to the PercentFrameLayout - i.e., if the PercentFrameLayout had a height of 100dp, a child view with a layout_heightPercent="12%" would be given 12dp. As your PercentFrameLayout has no other children and is wrap_content, there's no clear indication what 12% of an unspecified value is, leading to no space being allocated at all.

If you're trying to set the size of the view as a percentage of the total screen size or of the total visible height available to the RecyclerView, you'll have to do that elsewhere, ideally as a subclass of LinearLayoutManager as that is what is controlling the height of individual elements.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Just a note. 12% of 120dp is 14,4dp. – Gabriele Mariotti Nov 09 '15 at 07:48
  • @ianhanniballake Tried as u said. PercentFrameLayout to use height match_parent and its child imageview layout_heightPercent="12%" The image view is not showing :( I am trying to provide 12% of screen height not the 12% of recycler view. Is there anything I am missing here. – Amjed Baig Nov 09 '15 at 16:22
  • @AmjedBaig - `match_parent` won't work either because the `RecyclerView` is attempting to be as small as possible, so it is laying out each component with an unspecified max height (remember, they scroll). Again, you're trying to do 12% of an unspecified number. As I mentioned in the second paragraph, you cannot use this approach at all if you want items to take up a percentage of the screen size. You'll have to set the items height manually after [getting the screen dimensions](http://stackoverflow.com/a/1016941/1676363) – ianhanniballake Nov 09 '15 at 17:24
  • Thanks @ianhanniballake : I was doing that previously(as mentioned by you above, in the recycler adapter while binding the item) I thought with layout_heightPercent I can do it more gracefully. thanks for the help. – Amjed Baig Nov 10 '15 at 18:45