Asset Bekbossynov
Asset Bekbossynov

Reputation: 1713

Programmatically set margin to ConstraintLayout

I need to change margin of toolbar, which was made of ConstraintLayout, in different cases. I tried to do it in following way

ConstraintLayout.LayoutParams newLayoutParams = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
ConstraintLayout.MarginLayoutParams layoutParams = new ConstraintLayout.MarginLayoutParams(newLayoutParams);
layoutParams.setMargins(0, 0, 0, 0);
toolbar.setLayoutParams(newLayoutParams);

but in second case layoutParams.setMargins(16, 16, 16, 16); it did not change. So, can someone give other way or point to the mistake. Thanks for spending time to my problem.

I tried to use newLayoutParams.setMargins(54, 54, 54, 0); this puts margin to left and right, but I still need to put margin on top of it.

Upvotes: 38

Views: 47400

Answers (5)

Norbert
Norbert

Reputation: 1244

I once modified a ConstraintLayout with a ConstraintSet to change the elements it got linked with. I also changed the margin within that process which is the last parameter of the connect method:

ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(layout);
constraintSet.connect(
    R.id.layout_id, 
    ConstraintSet.START, 
    R.id.layout_id2, 
    ConstraintSet.END, 
    8);
constraintSet.applyTo(layout);

Maybe this gives you another hint to find a solution?

Upvotes: 13

Tobi Oyelekan
Tobi Oyelekan

Reputation: 372

good way to add margin to a view in constraintlayout

val constraintSet = ConstraintSet()
constraintSet.clone(constraintLayoutId)
val marginTop = context.getDimension(10f)
constraintSet.setMargin(R.id.tv_user, ConstraintSet.TOP, marginTop)
constraintSet.applyTo(constraintLayoutId)

fun Context.getDimension(value: Float): Int {
    return TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP, value, this.resources.displayMetrics).toInt()
}

Upvotes: 20

6rchid
6rchid

Reputation: 1292

For layoutParams.setMargins(w, x, y, z) the reason it's not changing is because you're most likely treating the values as Dp's rather than Px's, for which the method accepts.

To get Px from the Dp, you want to use:

int pxValue = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());

Upvotes: 2

Asset Bekbossynov
Asset Bekbossynov

Reputation: 1713

Finally with help of @Aksh I found my mistake and solve my problem. If someone find it useful, I will put my code bellow

     ConstraintLayout.LayoutParams newLayoutParams = (ConstraintLayout.LayoutParams) toolbar.getLayoutParams();
     newLayoutParams.topMargin = 0;
     newLayoutParams.leftMargin = 0;
     newLayoutParams.rightMargin = 0;
     toolbar.setLayoutParams(newLayoutParams);

Upvotes: 75

Aksh
Aksh

Reputation: 323

You might wanna try

ViewGroup.MarginLayoutParams params = yourConstraintLayout.getLayoutParams();
params.topMargin = x;
//others shows up in suggestion

I haven't tried it though I believe it should work as ConstraintLayout is a ViewGroup.

Upvotes: 4

Related Questions