Android and setting width and height programmatically in dp units
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In Android code, layout width and height are applied in pixels, even though designers and XML layouts usually describe sizes in dp. That means runtime resizing always starts with a unit conversion step. The important part is not just changing layoutParams, but doing it with the right density calculation and the right parameter type for the parent layout.
Convert dp to Pixels Correctly
A dp value is density-independent. layoutParams.width and layoutParams.height are not. To bridge that gap, convert dp to pixels using the device display metrics.
In Kotlin, a compact helper looks like this:
Using Android's built-in conversion avoids mistakes such as hard-coding a density multiplier or assuming all screens map 1dp to the same pixel count.
Update Layout Params at Runtime
Once you have pixel values, modify the view's layoutParams and assign them back to the view.
That is enough for simple size changes. Android will request layout again after the updated parameters are applied.
Use the Correct LayoutParams Subclass
If you only need width and height, the generic ViewGroup.LayoutParams fields are enough. If you also need margins, gravity, or constraint rules, the actual parent layout starts to matter.
For example, margins require a subclass that supports them:
If the parent is a ConstraintLayout, LinearLayout, or FrameLayout, each has its own parameter class for layout-specific behavior. Width and height may still work on the base type, but anything more advanced usually needs the correct subclass.
Know When to Use Layout Constants Instead
Not every dimension should be expressed as dp. Sometimes the correct runtime value is MATCH_PARENT or WRAP_CONTENT, just as it would be in XML.
This matters because developers sometimes over-convert everything to pixels when the real layout rule is about relationship to content or parent size, not fixed physical size.
Timing Matters for Some Layout Changes
If you are setting a fixed dp size, changing layout params in onCreate often works fine. If the new size depends on measured width, measured height, or the final parent layout, run the code later using post, a layout listener, or another callback that fires after measurement.
This is a different case from dp conversion, but it often appears in the same code path and causes confusion if the view has not been measured yet.
Prefer XML for Static Sizes
Programmatic resizing is appropriate when the dimension depends on runtime logic such as feature flags, user interaction, downloaded content, or dynamically created views. If the size is fixed for all users, XML is usually the better place because it is easier to inspect and maintain.
A good rule is: if the size never changes after layout inflation, put it in XML. If it truly depends on code-time decisions, set it programmatically.
Common Pitfalls
The most common mistake is assigning a dp number directly to layoutParams.width or layoutParams.height. Android interprets that number as pixels, so the view ends up too small or too large depending on screen density.
Another problem is casting to the wrong LayoutParams class. A cast that does not match the parent layout will fail at runtime or will hide the fields you actually need.
Developers also forget to reassign the modified params back to the view. Updating the local params object alone is not enough.
Summary
- Android layout params use pixels at runtime, not
dp. - Convert
dpto pixels withTypedValue.applyDimensionor an equivalent helper. - Update the view's
layoutParamsand assign them back to the view. - Use the correct
LayoutParamssubclass when you need margins or layout-specific rules. - Prefer XML for fixed sizes and runtime code only when the size really depends on runtime behavior.

