29

I am using PercentRelativeLayout from android percent support package. this is what I have in my layout.

<android.support.percent.PercentRelativeLayout
    android:id="@+id/view_parent"
    app:layout_heightPercent="68%" android:visibility="invisible"
    android:layout_width="match_parent"
    android:background="@color/mountain_meadow" android:layout_alignParentTop="true"
    android:layout_height="wrap_content">


    <View android:id="@+id/view_child" android:layout_width="match_parent" app:layout_heightPercent="30%"
        android:layout_alignParentBottom="true"
        />

</android.support.percent.PercentRelativeLayout>

I want to change the height of view_child programmatically. How can I do that using PercentRelativeLayout.LayoutParams or any other way.

Cœur
  • 37,241
  • 25
  • 195
  • 267
g.revolution
  • 11,962
  • 23
  • 81
  • 107

1 Answers1

68

Do you want to set a new percentage value? If yes, you need to:

View view = findViewById(R.id.view_child);
PercentRelativeLayout.LayoutParams params = (PercentRelativeLayout.LayoutParams) view.getLayoutParams();
// This will currently return null, if it was not constructed from XML.
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.heightPercent = 0.60f;
view.requestLayout();
gruszczy
  • 40,948
  • 31
  • 128
  • 181
  • How set another value without percent? for example, marginBottom in dp, using Percent layout params? – Gilian Apr 22 '17 at 17:15
  • hi @gruszczy, Do you know why I am always getting PercentLayoutInfo as 'null' ? Please help me. – Saamzzz May 06 '18 at 17:15
  • Hi @gruszczy, as per your comment '// This will currently return null, if it was not constructed from XML.'. Please help me adding this in XML – Saamzzz May 07 '18 at 12:18