3

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

Hans
  • 387
  • 1
  • 7
  • 20
Md.Tarikul Islam
  • 1,241
  • 1
  • 14
  • 16

5 Answers5

4

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.

sticky bit
  • 36,626
  • 12
  • 31
  • 42
Snehal
  • 543
  • 7
  • 11
3

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.

privatestaticint
  • 854
  • 1
  • 9
  • 23
1

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 :)

Community
  • 1
  • 1
Rachik Abidi
  • 156
  • 8
0

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: enter image description here

In this folder categorized in different screen size and different screen resolution.

Md.Tarikul Islam
  • 1,241
  • 1
  • 14
  • 16
0

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

Vaycent
  • 96
  • 6