Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8124201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:23:30+00:00 2026-06-06T06:23:30+00:00

My Android app looks great at 480×800 pixels resolution but it breaks at LDPI

  • 0

My Android app looks great at 480×800 pixels resolution but it breaks at LDPI screen with 240×400 pixels. I know I can get scaling factor of screen in activity using getResources().getDisplayMetrics().density which would give the exact float value depending upon screen resolution.

In my app, I have used RelativeLayout and ImageViews within it, and set the margins of images using setMargins(), as this method uses pixels in int for its top, left, bottom and right values, how can I set these values using DPI factor I got? (which is a float). We have Math.floor() but I’m positioning images at very particular locations using pixels, and DPI factor returned by method has very specific value at decimal places which can’t be ignored, so casting to int doesn’t work for me.

Update

Also, I use lp.setMargins(100,200,0,0); to set the margins, what modification I’ll be making in it to use DPI factor I recieved from device?

P.S. I know its more a mathematical issue rather than programming, but I’m poor with math, sorry.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:23:33+00:00Added an answer on June 6, 2026 at 6:23 am

    Looks like I was too early to ask the question instead of fiddling myself, but also noticed that I’m not AT ALL the first to face this issue.

    So, here’s solution I found.

    I created a following method in my Activity class.

    private int px(float dips)
    {
        float DP = getResources().getDisplayMetrics().density;
        return Math.round(dips * DP);
    }
    

    This would convert given dp value into appropriate pixels. You can make the variable DP used in method to be global and initialize it in your onCreate() method rather than within px(), just to make method execute fast.

    Usage:

    lp.setMargins(px(100),px(200), 0, 0);
    

    OR, if you don’t create px() just for this and have DP as global.

    lp.setMargins(100 * Math.round(DP), 200 * Math.round(DP), 0, 0);
    

    However, using method makes it look much cleaner and readable. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created app in android, everywhere looks great, but on the phone which has
I have a web app built with iweb kit that looks great in Android
we have developed and Android app, first time, great fun. Now we are giving
I am making an android app that shows the Calendar day view, it looks
I am developing an Android app (see screenshots). I have a layout that looks
I am developing an Android app (see screenshots). I have a layout that looks
I'm developing an app for Android 2.2 and higher. I use the great ActionBarSherlock
I have an Android app that looks absolutely horrible if the user sets their
I'm writing testcases for an android app extending ActivityInstrumentationTestCase2 . The test class looks
My Android app has a layout that looks like this: --------------------- | | |

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.