2

It is possible to use PercentRelativeLayout specifying only the desired width dimension and getting automatic heigh?

I mean doing this:

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
    android:id="@+id/earthImageView"
    app:layout_widthPercent="30%"
    android:src="@drawable/earth"
    android:layout_alignParentRight="true"
    android:adjustViewBounds="true"/>
</android.support.percent.PercentRelativeLayout >

The view has the correct width but if I check the layout boundaries I can see that the height is MATCH_PARENT. It means hat android:adjustViewBounds="true" is not working.

I tried adding app:layout_widthPercent="WRAP_CONTENT" but has no effect.

Edit

Also having problems when doing the inverse situation:

<ImageView
        android:id="@+id/earthImageView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_heightPercent="20%"
        android:src="@drawable/earth"
        android:adjustViewBounds="true"/>
halfer
  • 19,824
  • 17
  • 99
  • 186
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

1 Answers1

1

If I understand correctly you want to specify the width and have the height set automatically to whatever adjustViewBounds gives you.

If that's the case, the following should do what you want (I've set both of the layout and imageView heights to wrap_content):

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/earthImageView"
        android:layout_height="wrap_content"
        app:layout_widthPercent="30%"
        android:src="@drawable/lapin"
        android:layout_alignParentRight="true"
        android:adjustViewBounds="true"/>
</android.support.percent.PercentRelativeLayout >
mVck
  • 2,910
  • 2
  • 18
  • 20
  • btw if i try it with height percent instead of width... i got the same error. i added the new problem in the question, please can u test it? – NullPointerException Feb 13 '17 at 20:53
  • That's a good question @NullPointerException, can't seem to have it work either. Do you know the ratio of your image? If so you could remove the layout_width (and layout_height) and use 'app:layout_aspectRatio="80%"' for example – mVck Feb 13 '17 at 21:09
  • well, i will give this answer a positive vote btw mate, thanks a lot. One more thing please. I'm trying to use 150% on widthPercent but does not work. Maximum is 100%. Is it possible to use higher Percents than 100 on xml PercentRelativeLayout? – NullPointerException Feb 13 '17 at 21:25
  • Thanks. I don't think you can do that @NullPointerException (AFAIK you can use a value greater than 100% only for the aspectRatio) – mVck Feb 13 '17 at 21:46