You need to convert your value to dps, you can use the following function to do so:
public static int dpToPx(int dp, Context context) {
float density = context.getResources().getDisplayMetrics().density;
return Math.round((float) dp * density);
}
Then, to set the ImageView
size to the px value, you can do this:
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)imageView.getLayoutParams();
params.width = dpToPx(45);
params.height = dpToPx(45);
imageView.setLayoutParams(params);
(Change LinearLayout
for whatever container your ImageView
is in)
Edit: Kotlin Version
The function to convert to Px can be written like this in kotlin (as an extension)
fun Int.toPx(context: Context) = this * context.resources.displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT
Then can use it like this:
view.updateLayoutParams {
width = 200.toPx(context)
height = 100.toPx(context)
}