By user256239


2010-03-09 03:29:09 8 Comments

Does setWidth(int pixels) use device independent pixel or physical pixel as unit? For example, does setWidth(100) set the a view's width to 100 dips or 100 pxs?

Thanks.

6 comments

@Abhishek Garg 2019-08-27 12:27:08

Based on above answers which works fine to me, i generate some helper methods, just add them in your utils to use them in whole project.

   // value in DP
   public static int getValueInDP(Context context, int value){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }

    public static float getValueInDP(Context context, float value){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, context.getResources().getDisplayMetrics());
    }

    // value in PX
    public static int getValueInPixel(Context context, int value){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, value, context.getResources().getDisplayMetrics());
    }

    public static float getValueInPixel(Context context, float value){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, value, context.getResources().getDisplayMetrics());
    }

@Dan Lew 2010-03-09 05:06:18

It uses pixels, but I'm sure you're wondering how to use dips instead. The answer is in TypedValue.applyDimension(). Here's an example of how to convert dips to px in code:

// Converts 14 dip into its equivalent px
Resources r = getResources();
int px = Math.round(TypedValue.applyDimension(
    TypedValue.COMPLEX_UNIT_DIP, 14,r.getDisplayMetrics()));

@Romain Guy 2010-03-09 05:08:41

You can also call nbDips * getResources().getDisplayMetrics().density

@Dan Lew 2010-03-09 05:15:34

In fact, that's exactly what TypedValue.applyDimension() does. I considered posting that, but I like going through the API in case anything ever changes... though I guess it's safe to use that method if you give it the okay. :)

@Romain Guy 2010-03-09 05:25:02

The applyDimension() version executes more code and it a bit more difficult to read I find. FWIW, the line I posted is what we actually use all over the place in the framework and standard apps.

@Jonathan 2011-01-17 15:15:55

Hi, i tried the above two examples and it doesnt seem to calculate the pixels correctly. instead, it returns me the exact value i specify in my view: android:layout_width="50dip" in my case. it will return me 50 the exact value in the xml file. i then checked this in the app and as expected, the value is incorrect and draws the view too large then expected

@A-Live 2012-12-13 13:42:04

The only solution I found for px to pt conversion, very nice.

@Soon Santos 2018-09-26 20:15:05

Resources.getSystem().getDisplayMetrics().denisty in case you are in a Util class that has no context. Got this from here

@ansh sachdeva 2019-09-12 07:30:20

its weird when your team also has web developers. My Senior gives me a size of 50 * 50 px, so he is expecting me to have a 50*50 size button(" the size of a launcher icon"). But in android, its 50*50 dp , so he actually means he is giving me 50*50 dp size and i have to convert it into pixels which is a relatively bigger value.

@pomber 2015-02-14 16:13:43

float dps = 100;
float pxs = dps * getResources().getDisplayMetrics().density;

Source (@Romain Guy)

@Nidhin 2014-11-01 06:35:38

Method setWidth(100), set 100 px as width(not in dp).So you may face width varying problems on different android phones.So use measurement in dp instead of pixels.Use the below code to get measurement in dp of sample width=300px and height=400px.

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

int Height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics());

@SDJMcHattie 2013-02-17 14:15:27

The correct way to obtain a constant number of DIPs in code is to create a resources XML file containing dp values a bit like:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="image_width">100dp</dimen>
    <dimen name="image_height">75dp</dimen>
</resources>

Then refer to the resource in your code like so:

float width = getResources().getDimension(R.dimen.image_width));
float height = getResources().getDimension(R.dimen.image_height));

The float you have returned will be scaled accordingly for the pixel density of the device and so you don't need to keep replicating a conversion method throughout your application.

@Wayne Phipps 2013-06-07 11:02:51

In my situation, this solution works best where I have an custom dialog which has a listview and a want to set the width of the Header Labels and columns in the ListView to the same width.

@Joffrey 2014-06-19 08:35:21

Your first sentence is kinda funny, you are basically saying that creating an XML file is the proper way to "specify [something] in code". Well I think you're merely using the constant in code not specifying it :)

@SDJMcHattie 2014-07-10 14:37:12

@Joffrey yes I see what you mean so I've updated the wording slightly

@Josnidhin 2010-03-09 04:33:16

Pixels of course, the method is asking for pixels as parameter.

@user256239 2010-03-09 05:05:12

If it is in layout xml file, we can specify android:layout_width="100dip" or android:layout_width="100px". In the source code, we can't specify the width of a layout as 100dip?

@Romain Guy 2010-03-09 05:10:25

Not directly, you have to convert yourself using DisplayMetrics.density.

Related Questions

Sponsored Content

34 Answered Questions

[SOLVED] Converting pixels to dp

40 Answered Questions

[SOLVED] How to get screen dimensions as pixels in Android

11 Answered Questions

33 Answered Questions

4 Answered Questions

[SOLVED] Android and setting width and height programmatically in dp units

  • 2011-03-10 03:38:51
  • SK9
  • 188752 View
  • 309 Score
  • 4 Answer
  • Tags:   android scaling

5 Answered Questions

[SOLVED] Programmatically set height on LayoutParams as density-independent pixels

10 Answered Questions

[SOLVED] Div width 100% minus fixed amount of pixels

  • 2009-03-16 17:14:24
  • Removed_account
  • 243596 View
  • 319 Score
  • 10 Answer
  • Tags:   html css height width

3 Answered Questions

[SOLVED] How to compute device independent pixel units?

4 Answered Questions

[SOLVED] Access to hardware pixels on mobile devices

2 Answered Questions

[SOLVED] Maximum dp/dip on android devices

Sponsored Content