I want to programmatically set in my layout size. And this layout size get from dimen folder. but the problem is when I get the value from dimen folder, It gets displayed in a normal resolution screen perfectly but when it runs in a high display resolution it doesn't run perfectly
-
1getResources().getDimensionPixelSize(R.dimen.YOUR_DIMEN) doesn't help you? – Amir Aug 30 '16 at 19:57
5 Answers
You can set layout size programmatically by using one of the following ways:
Method 1:
int width = getResources().getDimensionPixelSize(R.dimen.layout_width);
int height = getResources().getDimensionPixelSize(R.dimen.layout_height);
Method 2:
int width = (int) (widthdpValue * Resources.getSystem().getDisplayMetrics().density);
int height = (int) (heightdpValue * Resources.getSystem().getDisplayMetrics().density);`
Set this width and height to the layout.

- 36,626
- 12
- 31
- 42

- 543
- 7
- 11
You have to convert the dp value you get from the dimen
folder into the proper number of pixels for the device's screen density. Example:
float dp = 5.0f // This is the dp value from your dimen file
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
Where px
is the scaled pixel value that will properly display on the device.

- 854
- 1
- 9
- 23
Refer to this link Different values folders in android!
Create a "values" folder for each screen density and define the size of your layout in each "dimensions.xml" file. This will give you the ability to use different sizes for the same layout without using any piece of Java code.
Hope this helps :)

- 1
- 1

- 156
- 8
Yes I solved my screen resolution
problem.
When you get value from dimen folder it get in Pixel format. And Pixel size depends on your device resolution
Not device size
.
So you need create screen size
and screen resolution
based dimen folder. As like:
In this folder categorized in different screen size and different screen resolution.

- 1,241
- 1
- 14
- 16
You can't use only one dimens for all the devices layout, please try to create some value resouce folder with different size
And u can refer to my project as below which can help u to gen the dimens in different floder automatically https://github.com/Vaycent/genDimensXml I hope this can help u

- 96
- 6