javaandroidandroid-layoutandroid-percentrelativelayout

Set PercentRelativeLayout percentage in Java


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.


Solution

  • 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;
    }