0

Why dp used in the XML files are not the same as dp used in activities? When I create a textView in the XML file with 20dp of width, its size will be the same in all different type of screens, but when I use this:

int sizeInDip = 20;
int width= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInDip, getResources().getDisplayMetrics());

The size is different in all types of screen.

I want to know if there's a way to get the same result using dp in both, activities and XML files.

Thank you in advance!

anonymous
  • 1,320
  • 5
  • 21
  • 37

1 Answers1

0

Why are you doing it that way? IIRC, theres a TextView.setSize method you can invoke programatically and just set the size as 20dp that. Also, you should use sp(scaleable pixels) for text sizes

Jade Byfield
  • 4,668
  • 5
  • 30
  • 41
  • It was only an example... What if I want to use `sizeInDip` for images, paddings, margins, layouts etc etc. And no, I cannot use `20dp` inside setTextSize cuz I need to introduce an `int` only... – anonymous Apr 16 '12 at 16:38
  • setTextSize() is fine as it takes a float but it is "interpreted as 'scaled pixels' " according to the documentation, which is what you need. – Jade Byfield Apr 17 '12 at 18:21
  • Thank you, but I'm asking for a general solution: paddings, margins, layouts, images etc... – anonymous Apr 17 '12 at 18:39