Android and setting width and height programmatically in dp units
You'll have to convert it from dps to pixels using the display scale factor.
final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (dps * scale + 0.5f);
Set ImageView Size Programmatically in DP Java
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)
}
How to set Layoutparams height/width in dp value?
When you specify values programmatically in the LayoutParams
, those values are expected to be pixels.
To convert between pixels and dp you have to multiply by the current density factor. That value is in the DisplayMetrics
, that you can access from a Context
:
float pixels = dp * context.getResources().getDisplayMetrics().density;
So in your case you could do:
.
.
float factor = holder.itemView.getContext().getResources().getDisplayMetrics().density;
params.width = (int)(item.getWidth() * factor);
params.height = (int)(item.getHeight() * factor);
.
.
What unit should I use to set imageView width and height in Android?
They're in pixels. All lengths and widths outside of xml are in pixels. You can convert DP to pixels by using Converting pixels to dp if you want to specify the size in dp.
Programmatically set height on LayoutParams as density-independent pixels
You need to convert your dip value into pixels:
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, <HEIGHT>, getResources().getDisplayMetrics());
For me this does the trick.
How can I set layout height and width programmatically which support all screen size in Android?
You can get the device density like this:
float scaledDensity = getResources().getDisplayMetrics().scaledDensity;
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(20 * scaledDensity,20 * scaledDensity);
param1.weight=1;
Related Topics
Location Service Gps Force Closed
What's the Android Adb Shell "Dumpsys" Tool and What Are Its Benefits
Emulator Error: This Avd's Configuration Is Missing a Kernel File
Get JSONarray Without Array Name
Google Play Game Services - Unable to Sign In
How to Make an Imageview in Circular Shape
Get the Uri of an Image Stored in Drawable
How to Use Both Ontouch and Onclick for an Imagebutton
What Is the Maximum Amount of Ram an App Can Use
Android Eclipse Ddms - Can't Access Data/Data/ on Phone to Pull Files
Get Center Visible Item of Recycleview When Scrolling
How Do Detect Android Tablets in General. Useragent
Android Get Orientation of a Camera Bitmap? and Rotate Back -90 Degrees
Passing Data from One Activity to Another Using Bundle - Not Displaying in Second Activity
Indoor Positioning System Based on Gyroscope and Accelerometer