Welcome! Log In Create A New Profile

Advanced

Android DPI fun

Posted by Bosher 
Bosher
Android DPI fun
July 31, 2018 06:40PM
Hi all

Been beating my head against a wall all day today but finally worked out how to handle getting the DPI of any android device.... To help anyone else who may suffer in the same way, here's how..

Identify a static control on your window (IMG_Menu in my case)

Since in the WM editor everything is always based on 160DPI you can use the CoordinateScreenToEditor command to work out what the DPI is.

all variables have 2 decimal places..
nX = CoordinateScreenToEditor(IMG_Menu..X)
gnDPIRatio = Round(IMG_Menu..X / nX,2)

MyDPI = 160 * gnDPIRatio

Boom!
Fabrice Harari
Re: Android DPI fun
July 31, 2018 07:50PM
Hi

it's certainly interesting, but it begs the question: why?

Best regards
Arie
Re: Android DPI fun
July 31, 2018 08:42PM
Bosher,

I once found this piece of code. I use it to display the "density" of the screen in some kind of system-info screen in my apps.
I think this is your gnDPIratio .

Just create a global procedure as follows
import android.app.Activity; 
import android.content.Context; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager;
import android.util.DisplayMetrics;

public static double getDisplayDensity()
{
	// [developer.android.com] 
	DisplayMetrics metrics = new DisplayMetrics();
	getActiviteEnCours().getWindowManager().getDefaultDisplay().getMetrics(metrics);
	
	return metrics.density;	
}
bosher
Re: Android DPI fun
July 31, 2018 10:02PM
Fabrice,

It's because I needed to change the fontsize in a listbox according to device resolution. Anchoring resizes the control fine, However, I compute the optimal fontsize using the textwidth trick just fine. The issue is that with a listbox you need to also change the lineheight property to get it to display correctly.

I've built an algorithm that does this quite well now.

The reason i'm using a listbox is because I need to alternate the colours of each line based on content.

It's a long story I know... :0

Cheers

Bob
bosher
Re: Android DPI fun
July 31, 2018 10:03PM
Thanks Arie.

That looks useful!

Cheers

Bob
Author:

Your Email:


Subject:


Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically. If the code is hard to read, then just try to guess it right. If you enter the wrong code, a new image is created and you get another chance to enter it right.
Message: