0

I am using below code to align imageview to bottom right with margin of 20,10 inside FrameLayout but it is appearing at top left.Where am I going wrong?

 ViewGroup.MarginLayoutParams marginParams = new ViewGroup.MarginLayoutParams(refreshBtn.getLayoutParams());
    marginParams.setMargins(0, 0, 20, 10);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(marginParams);
layoutParams.gravity=Gravity.RIGHT|Gravity.BOTTOM;
refreshBtn.setLayoutParams(layoutParams);
Android Developer
  • 9,157
  • 18
  • 82
  • 139

1 Answers1

-1

The problem is that you are changing the layout gravity, meaning that you are positioning the FrameLayout in the bottom right corner. However, it's possible that the FrameLayout is filling the whole screen in which case the layout gravity doesn't really change anything. Imagine a book perfectly fitting in a drawer. No matter how you're holding the drawer, the book doesn't move inside it.

What you have to do is use the "setGravity()" method of the FrameLayout itself. This will change the actual gravity inside the FrameLayout.

Source: How to set both gravity and layout gravity of a LinearLayout programatically

Community
  • 1
  • 1
IPat
  • 139
  • 1
  • 6