-1

In Android SDK there isn't an overload of a constructor of ViewGroup (and subclasses) with possibilities to specify the width and height in DP (or DIP) instead of in pixel. Why? Actually we have:

ViewGroup.LayoutParams(int width, int height)

Could be a good idea to have:

ViewGroup.LayoutParams(int dpWidth, int dpHeight, int ComplexUnitType.Dip) 

where ComplexUnitType could be an enum with units of measurement (for e.g)

aorlando
  • 668
  • 5
  • 21

1 Answers1

-1

You can use the method complexToDimensionPixelSize used in the TypedValue class , this method accepts the value to be convert to pixel and the current device metrics, so a possible implementation could be like this :

int width = 300; // value to be converted
DisplayMetrics metrics = getResources().getDisplayMetrics();
int widthInPx = TypedValue.complexToDimensionPixelSize(width, metrics);
Fouad Wahabi
  • 804
  • 7
  • 16
  • This is not what I have asked, The solution reported is well known, but my question is why this solution is not integrated in the android SDK – aorlando Jun 30 '15 at 09:55
  • @aorlando so in this case I think this is not the place to ask such questions , stackoverflow is for asking programming questions , so if you want to implement your own `LayoutParams` and use it with `ViewGroup`s this is the proper answer instead you have to find another place to ask this question – Fouad Wahabi Jun 30 '15 at 10:11
  • It is a programming question and I am interested in someone could give some suggestion on why this solution could create a probem, someone expert more than me in android – aorlando Jun 30 '15 at 16:16
  • @aorlando who told you that this could make a problem, if we think like the way you think , a lot of methods in the API should be changed, but unfortunately, despite the open source of the android API, there's no way to contribute or make a pull request – Fouad Wahabi Jun 30 '15 at 18:00
  • no one told me, for this reason I am asking; the way to contribute is specified here: https://source.android.com/source/contributing.html – aorlando Jul 01 '15 at 10:13
  • @aorlando contributing to AOSP is not the same like contributing to any other open source project, this is why the the "open source" terminology seems to be weird with Android ( you can read there's plenty of article discussing this ), so maybe you could report this as bug but I don't think that it could be treated as a bug – Fouad Wahabi Jul 01 '15 at 10:42
  • different of what? what articles? This is not a bug but a "feature" request already submitted to android platform group – aorlando Jul 01 '15 at 13:24
  • @aorlando you can read Karim Yaghmour book Embedded Android first chapter it discuss the open source terminology, anyway good luck , and I insist that this is not the place to post such questions ( suggestions ) – Fouad Wahabi Jul 01 '15 at 13:37
  • thanks for the suggestion, but this is a technical question for stackoverflow community – aorlando Jul 02 '15 at 16:38