0

Dose any one know how to change the densityDpi for an android app in java?

I'm working on an android application where i manipulate the target density in the viewpoint tag. This is causing a minor problem in some place of the apps, the target density seems to be reset by the soft keyboard.

So I need a way to reset this, or prevent the keyboard from changing the density.

R T
  • 195
  • 9
  • Changing the density? This sounds really weird and is likely a bad idea. What's the use case? – dmon Aug 09 '12 at 00:19
  • Unconventional for sure. an experiment at this point to see if this approach will work. we have a phonegap application that is over sized and are using the view port target density to scale down. when the soft keyboard zooms it appears to change the density to achieve the zoom effect, looking for a way to reset the density or prevent this density change from occurring. this seems to be the only bug with this approach, but having some major difficulty figuring it out. – R T Aug 09 '12 at 01:11
  • react to change in application onconfigchange? – Lassi Kinnunen Apr 22 '16 at 22:50

1 Answers1

0

For devices like the lenovo A1 (tablet with 240dpi density) or the galaxy note you may need to do this in your app to avoid everything looking like its at varying sizes across devices.

Add the following function to your extended application class and call it from the onCreate:

public void changeDensity(float desiredDensity) {
    //desiredDensity : ldpi = 0.75 (120dpi) , mdpi = 1 (160dpi), hdpi = 1.5 (240dpi), xhdpi = 2.0 (320dpi)
    DisplayMetrics metrics = getResources().getDisplayMetrics();

    metrics.density = desiredDensity;
    metrics.xdpi = desiredDensity * 160;
    metrics.ydpi = desiredDensity * 160;
    metrics.densityDpi = (int) (desiredDensity * 160);

    getResources().updateConfiguration(null, null);
}
petey
  • 16,914
  • 6
  • 65
  • 97