Skip to main content

converting pixels to dp in android



I have created my application with the height and width given in pixel for a pantech device whose resolution is 480x800.





I need to convert the the height and width for an G1 device. I thought converting it into dp will solve the prob and provide same solution for both the device.





is there any easy way to convert the pixels to dp?? or any suggestions??



Source: Tips4all

Comments

  1. /// Converts 14 dip into its equivalent px

    Resources r = getResources();
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, r.getDisplayMetrics());

    ReplyDelete
  2. According to the Android Development Guide:

    px = dp * (dpi / 160)

    But often you'll want do perform this the other way around when you receive a design that's stated in pixels. So:

    dp = px / (dpi / 160)

    If you're on a 240dpi device this ratio is 1.5 (like stated before), so this means that a 60px icon equals 40dp in the application.

    ReplyDelete
  3. /**
    * This method convets dp unit to equivalent device specific value in pixels.
    *
    * @param dp A value in dp(Device independent pixels) unit. Which we need to convert into pixels
    * @param context Context to get resources and device specific display metrics
    * @return A float value to represent Pixels equivalent to dp according to device
    */
    public static float convertDpToPixel(float dp,Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float px = dp * (metrics.densityDpi/160f);
    return px;
    }
    /**
    * This method converts device specific pixels to device independent pixels.
    *
    * @param px A value in px (pixels) unit. Which we need to convert into db
    * @param context Context to get resources and device specific display metrics
    * @return A float value to represent db equivalent to px value
    */
    public static float convertPixelsToDp(float px,Context context){
    Resources resources = context.getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    float dp = px / (metrics.densityDpi / 160f);
    return dp;

    }

    ReplyDelete
  4. You should use dp just as you would pixels. That's all they are; display independent pixels. Use the same numbers you would on a medium density screen, and the size will be magically correct on a high density screen.

    However, it sounds like what you need is the fill_parent option in your layout design. Use fill_parent when you want your view or control to expand to all the remaining size in the parent container.

    ReplyDelete
  5. PX and DP are different but similar.

    DP is the resolution when you only factor the physical size of the screen. When you use DP it will scale your layout to other similar sized screens with different pixel densities.

    Occasionally you actually want pixels though, and when you deal with dimensions in code you are always dealing with real pixels, unless you convert them.

    So on a android device, normal sized hdpi screen, 800x480 is 533x320 in DP (I believe). To convert DP into pixels /1.5, to convert back *1.5. This is only for the one screen size and dpi, it would change depending on design. Our artists give me pixels though and I convert to DP with the above 1.5 equation.

    ReplyDelete
  6. use something like this

    float sizeInDip = 10f;
    int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, sizeInDip, getResources().getDisplayMetrics());


    Naturally you’ll need access to your resources (context.getResources() etc.). Note that I’ve just cast this to an int here, you may want to round it up either.

    ReplyDelete
  7. float density = context.getResources().getDisplayMetrics().density;
    float px = someDpValue * density;
    float dp = somePxValue / density;


    'density' equals 1.0 on mdpi, 1.5 on hdpi and 0.75 on ldpi.

    ReplyDelete
  8. You can therefore use the following formulator to calculate the right amount of pixels for a dimension specified in dp

    public int convertToDp(int input) {
    // Get the screen's density scale
    final float scale = getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale
    return (int) (input * scale + 0.5f);
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.