Programmatically set margin to ConstraintLayout

android set margin programmatically
constraint layout params programmatically
set margin programmatically android kotlin
set margin programmatically android recyclerview
add view to constraintlayout programmatically
constraint layout parent programmatically
how to set relativelayout margin programmatically in android
set margin programmatically android to view

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);

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.

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;

ConstraintLayout set margin programmatically, android kotlin - ConstraintLayout set margin programmatically. MainActivity.kt. package com.example.jetpack import android.content.Context  Chaining Views in a ConstraintLayout Programmatically One of the biggest challenges of building elegant mobile interfaces is making them adapt properly to the full gamut of screen sizes. This is especially true for Android development.

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.connect(, ConstraintSet.START,, ConstraintSet.END, 8);

Maybe this gives you another hint to find a solution?

Android: programmatically adjust ConstraintLayout's layout , Android: programmatically adjust ConstraintLayout's layout Pro is you don't have to programmatically adjust size, constraints and margins. Now, if you set the margin using Layout_marginLeft, Right, etc, you need to update margin in this way. layoutParams.setMargins(left, top, right, bottom); If you set margin using the new layout_marginStart, you need to update margin in this way. layoutParams.setMarginStart(start); layoutParams.setMarginEnd(end); Step 2, to update margin in dp

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.

Adding Views & Constraints to Android Constraint Layout , layout, setting bias & view size, and baseline constraits programmatically. Using constraint layout, you can create flat layouts. Using ConstraintSet, you can add constraints, set bias, resize views, and create chains. and second one make it align to left side of the parent with constraint margin 18. How to create constraints programmatically with ConstraintLayout in Android. From API < 17, there is ViewCompat.generateViewId() For API 17, there is View.generateViewId().

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());

Set margins in dp programmatically – Android, Sometimes at runtime, it is required to set margin to a view. But, the problem is getting different size for different screen sizes, i.e. hdpi, xhdpi,  To set the margin for the guideline, you can call setGuidelineBegin() method or setGuidelineEnd() method on constraintset object passing guideline id and margin. For vertical guidelines, using these methods set left and right margins respectively and for horizontal guideline, these methods set top and bottom margins respectively.

good way to add margin to a view in constraintlayout

val constraintSet = ConstraintSet()
val marginTop = context.getDimension(10f)
constraintSet.setMargin(, ConstraintSet.TOP, marginTop)

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

Margin not working when add the button programmatically · Issue , flexbox programmatically, the margins seem not appear in the layout. set parameters to wrapContent .then setMargins will work finebut  Note: If you do not specify the gone margin and the view is set to gone then the margin considered for that will be zero. Take an advantage of percentage system The new concept is bias system what this does is that you will define the percentage of the screen the view should move forward or downward ranging for 0 to 100%.

Android set layout margin programmatically, LayoutParams. 4472429/change-the-right-margin-of-a-view-programmatically Adding Views & Constraints to Android Constraint Layout Programmatically. In this android programming source code example, we are going to explore how to add Views programmatically in ConstraintLayout in Android. Below is a step by step source code to add Views programmatically in ConstraintLayout in Android.

How to programmatically set View margins in dp in Kotlin, Below is a step by step source code to programmatically set margins in dp in Kotlin. activity_main.xml <androidx.constraintlayout.widget.ConstraintLayout  In this android kotlin source code example, we are going to programmatically set margins in dp in Kotlin. You can copy and adopt this source code example to your Kotlin android project without reinventing the wheel. Below is a step by step source code to programmatically set margins in dp in Kotlin

ViewGroup.MarginLayoutParams, See ViewGroup Margin Layout Attributes for a list of all child view Creates a new set of layout parameters. Sets the margins, in pixels. If margins are set, they will be taken in account in the computation (Fig. 8, (c) with 0dp). Important: MATCH_PARENT is not recommended for widgets contained in a ConstraintLayout . Similar behavior can be defined by using MATCH_CONSTRAINT with the corresponding left/right or top/bottom constraints being set to "parent" .