2

I want to change the width percentage of items inside a PercentRelativeLayoutdynamically in Java based on some events, I found this, but I cannot find where to put the percentages:

mLinerLayout.setLayoutParams(new PercentRelativeLayout.LayoutParams());

My xml looks something like this:

<android.support.percent.PercentRelativeLayout
    android:id="@+id/ad_prl_tags"
    android:layout_width="match_parent"
    android:layout_height="48dp">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/ad_rv_tags"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        app:layout_widthPercent="70%" />

    <!--Add Tags Button-->

    <LinearLayout
        android:id="@+id/ad_ll_add_tags_container"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/ad_rv_tags">

        <ImageView
            android:id="@+id/itd_animatable_plus_iv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <EditText
            android:id="@+id/ad_et_add_tags_and_info"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>
</android.support.percent.PercentRelativeLayout>

I want to set the width percentage of the LinearLayout to 70% and the width of the RecyclerView to 30% in Java.

Onik
  • 19,396
  • 14
  • 68
  • 91
Vlad
  • 988
  • 12
  • 18
  • 1
    I was curious about this too, so I looked at the percent layout code. Turns out that it's only set up for getting the percentages from the layout XML file. Rather than figure out how to programmatically create an `AttributeSet` as if it was parsed from XML, you might be better off creating a custom `LinearLayout` class for a wrapper and override `onLayout` to do the percentage calculations yourself. – kris larson Mar 10 '16 at 12:31

1 Answers1

4

You can use it pragmatically with this method :

public static void setWidthPercent(View view, float width) {
    PercentLayoutHelper.PercentLayoutParams params =(PercentLayoutHelper.PercentLayoutParams) view.getLayoutParams();
    PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
    info.widthPercent = width;
}
Rami
  • 158
  • 1
  • 8