how to set left and top margin to a frame layout programmatically?

680 Views Asked by At

I have a framelayout inside a relative layout and i want to move frame layout a bit to the left and top as a whole. I have been using setLeft() and setTop() method. It is stretching the view but still it is okay for my purpose. My problem is I want the original unstretched frame layout back when I want it. But setting setRight() and putting the same parameter doesn't seem to work. Please help.

2

There are 2 best solutions below

0
Murat Çakır On

You can use setMargin to your framelayout

val par = framelayout?.layoutParams as FrameLayout.LayoutParams

par.rightMargin = 10
par.leftMargin = 30
0
Mohanraj On

You can't set Margin to your view like set padding. You need a Layout params for your frame layout then set margin to layout param.

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(5, 5, 5, 5);
frameLayout.setLayoutParams(layoutParams);