0

I am a beginner in android.I am making a app which has most of its contents generated dynamically. When I am setting size of any content in pixels it looks good on my device (on which i m testing) but its size does not remain the same when I run on other devices(because of different dpi). How to set size of the contents in dp such that it resizes itself as the contents which are created statically ?

Example - On my device I set size of some button as 50 px. how to set the size such that it resizes accordingly ? Thanks in advance.

Kabir
  • 1
  • 2
  • use wrap content in both height and width in XML. – Ashish Kudale Jun 20 '16 at 18:33
  • so suppose if on a 320 dpi screen , to set size of 25 dp i need to set size as 50 pixels and on a 480 dpi screen, to set size of 25 dp I need to set size as 75 pixels ? – Kabir Jun 20 '16 at 19:02

3 Answers3

0

Here is the answer you need.

In short, use:

/// Converts 14 dip into its equivalent px

Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, r.getDisplayMetrics());
Community
  • 1
  • 1
Saeed Jassani
  • 1,079
  • 2
  • 11
  • 27
  • so suppose if on a 320 dpi screen , to set size of 25 dp i need to set size as 50 pixels and on a 480 dpi screen, to set size of 25 dp I need to set size as 75 pixels ? – Kabir Jun 20 '16 at 19:03
  • No... Just specify the dps... It will automatically convert according to the different devices – Saeed Jassani Jun 20 '16 at 19:08
0

You need to add a utility function for converting dp into pixels.

  public static int pxFromDp(final Context context, final float dp) {
        return (int)(dp * context.getResources().getDisplayMetrics().density);
    }
Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
  • so suppose if on a 320 dpi screen , to set size of 25 dp i need to set size as 50 pixels and on a 480 dpi screen, to set size of 25 dp I need to set size as 75 pixels ? – Kabir Jun 20 '16 at 19:02
  • No nothing like that, you need to just set the pixels as pxFromDp(25) ...... 50 / 75 will automatically calculated by this function. – Arpit Ratan Jun 20 '16 at 19:05
  • but I tried to set size as (25 * (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics())) but its not working correctly and looking almost similar to your function. are these both different ? – Kabir Jun 20 '16 at 19:10
  • you should use it like this ..... TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 25, r.getDisplayMetrics()); – Arpit Ratan Jun 20 '16 at 19:14
0

You will need to convert it from dps to px using the display scale factor.

final float scale = getContext().getResources().getDisplayMetrics().density;
int pixels = (int) (dps * scale + 0.5f);

and then set the pixel value dynamically.

This formula is also given in android docs, for further reading please check the following link:

https://developer.android.com/guide/practices/screens_support.html#screen-independence

AmanSinghal
  • 2,404
  • 21
  • 22
  • so suppose if on a 320 dpi screen , to set size of 25 dp i need to set size as 50 pixels and on a 480 dpi screen, to set size of 25 dp I need to set size as 75 pixels ? – Kabir Jun 20 '16 at 19:03
  • No you don't need to change size according to the screen dp, you just pass the value to the dps variable in the formula and it will automatically compute the actual value at runtime. – AmanSinghal Jun 20 '16 at 19:05
  • but I tried to set size as (25 * (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, getResources().getDisplayMetrics())) but its not working correctly and looking almost similar to your function. are these both different ? – Kabir Jun 20 '16 at 19:13
  • I am not sure, try both and check. – AmanSinghal Jun 20 '16 at 19:17