Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set PercentageRelativeLayout margin programmatically

Tags:

android

Please, how do I set the percentage margin for the imageview programmatically.

<android.support.percent.PercentRelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/rating_thumb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/thumb" />

</android.support.percent.PercentRelativeLayout>
like image 238
ayz4sci Avatar asked Jan 28 '16 16:01

ayz4sci


1 Answers

After several hours of googling and tweaks, I finally got the answer.

imageView =  (ImageView) view.findViewById(R.id.rating_thumb);
PercentRelativeLayout.LayoutParams layoutParams = (PercentRelativeLayout.LayoutParams) imageView.getLayoutParams();
PercentLayoutHelper.PercentLayoutInfo percentLayoutInfo = layoutParams.getPercentLayoutInfo();
percentLayoutInfo.leftMarginPercent = 15 * 0.01f; //15 is the percentage value you want to set it to
imageView.setLayoutParams(layoutParams);
like image 196
ayz4sci Avatar answered Oct 20 '22 01:10

ayz4sci