Use ViewGroup.LayoutParams .
LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); myview.setLayoutParams(lp);
You can also specify fixed pixel sizes in the constructor instead of constants. But fixed pixels are a bad idea for Android because there are so many devices.
You can calculate pixels from dp size, although this is normal:
float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources.getDisplayMetrics());
(Here: convert 10dp to pixel value)
user658042
source share