0

I have a Floating Action Button, whose position I want to change programmatically

<com.google.android.material.floatingactionbutton.FloatingActionButton
   android:id="@+id/expandViewButton"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:clickable="true"
   android:visibility="visible"
   android:layout_marginStart="168dp"
   app:layout_constraintStart_toStartOf="@+id/coordinatorLayout1"
   app:layout_constraintTop_toTopOf="@+id/coordinatorLayout1"
   app:srcCompat="@mipmap/ic_launcher_round" />

I used the following code to change position programmatically.

ConstraintLayout.LayoutParams layoutParams=new 
ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT,ConstraintLayout.LayoutParams.WRAP_CONTENT);
int left,right,top,bottom;
left=(int)MainActivity.device_width/2;
right=0;
top=(int) MainActivity.device_height-200;
bottom=0;
layoutParams.rightMargin=right;
layoutParams.topMargin=20;
layoutParams.leftMargin=left;
layoutParams.bottomMargin=bottom;
   
layoutParams.bottomToBottom=R.id.fb1;
layoutParams.startToStart=R.id.fb1;

However layoutParams.rightMargin, layoutParams.topMargin, layoutParam.leftMargin and layoutParams.bottomMargin seem not to be working. Kindly suggest me a solution.

Mark Rotteveel
  • 82,132
  • 136
  • 114
  • 158
  • @Jameido layoutParams.topMargin not working – Amit Bhatiwal May 06 '21 at 10:00
  • It might helpful follow this link: [https://stackoverflow.com/questions/52148129/programmatically-set-margin-to-constraintlayout](https://stackoverflow.com/questions/52148129/programmatically-set-margin-to-constraintlayout) – Muhammad Ammar May 06 '21 at 16:15

0 Answers0