Q:

Android Studio constraint layout change absolute

i need to edit absolute values of ImageView:
tools:layout_editor_absoluteY="0dp"

to
tools:layout_editor_absoluteY="50dp"

but in java if I just do
ImageView img = (ImageView)findViewById(R.id.mainView);
ConstraintLayout.LayoutParams params1 = (ConstraintLayout.LayoutParams)(img.getLayoutParams());
params1.editorAbsoluteY = 50;
img.setLayoutParams(params1);

it doesn't work.

A:

Convert your value from DP to pixels:
public int dpToPx(int dp) {
    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
    return Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

before setting the params1.editorAbsoluteY.

