Know if your app is running on a phone or tablet

Monkey Targets Forums/Android/Know if your app is running on a phone or tablet

Supertino(Posted 2012) [#1]
Is there a way of knowing if you app is running on a Tablet or phone?

There are a lot of cheap 7" tablets out there that run at 800x480 same as my 3.7" phone and some expensive 4-5" phones that run at 1280x768 same as my 7" nexus 7.

Would be nice to know the screen dimensions or DPI so I can scale text etc appropriately.


Xaron(Posted 2012) [#2]
http://developer.android.com/guide/practices/screens_support.html#testing

http://developer.android.com/about/dashboards/index.html


Ironstorm(Posted 2012) [#3]
I'm using this little function to detect if my app is running on a tablet or phone.



Worked for me on my HTC OneX (1280px x 800px), Acer Iconia Tab A501 (1280px x 800px) and HTC Desire S (800px x 480px)


therevills(Posted 2012) [#4]
What happens when Mobile screens have the same resolution/DPI as Tablets? ;)

This is from Google's own code:

    public static boolean isTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout
                & Configuration.SCREENLAYOUT_SIZE_MASK)
                >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }


http://code.google.com/p/iosched/source/browse/android/src/com/google/android/apps/iosched/util/UIUtils.java

Really you are after is the current device has a large screen, not if its a tablet or not.

[Edit - Just found this:

https://groups.google.com/forum/?fromgroups=#!topic/android-developers/1jI9gfIm-T8

DisplayMetrics metrics = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(metrics);

double inches = Math.sqrt((metrics.widthPixels * metrics.widthPixels) + (metrics.heightPixels * metrics.heightPixels)) / metrics.densityDpi;

if (inches > TABLET_SIZE) {
   this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}


This code outputs the inches as:
Galaxy Nexus = 4.3
Droid = 4.08
Nexus 7 = 6.9
Samsug 10.1 = 9.2


Supertino(Posted 2012) [#5]
Thanks all, yes it looks like I need to return the actual size of the device screen in Inches regardless of resolution. I assume that last code junk is easy enough to plug into Monkey.


therevills(Posted 2012) [#6]
I assume that last code junk is easy enough to plug into Monkey.


Yeah, it should be something like this (not tested):

Monkey code:
Import "android.java"

Extern
	Function GetDeviceSize:Float() = "extern.getDeviceSize"

Public


android.java
class extern
{
	static float getDeviceSize()
	{
		DisplayMetrics metrics = new DisplayMetrics();

		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		double inches = Math.sqrt((metrics.widthPixels * metrics.widthPixels) + (metrics.heightPixels * metrics.heightPixels)) / metrics.densityDpi;
		return  (float)inches;
	}
}



Supertino(Posted 2012) [#7]
Thanks rev I'll give it a bash when I am back home on Friday.


Midimaster(Posted 2013) [#8]
did anybody ever use or try these codes above?

I try to start them, but I get massive error messages:

class Display {
	static int GetDpi() {
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int dpi= metrics.densityDpi;
		return  dpi;
	}
}



Strict
Import mojo
Import "SystemInfo.java"


Extern
	#If TARGET="android"
		Function GetExtDpi:Int() = "Display.GetDpi"
	#Endif
Public




At the end I found this:

http://stackoverflow.com/questions/10955287/using-getwindowmanager-inside-broadcastreceiver


Midimaster(Posted 2013) [#9]
A lot of confusion in this thread, but I found a solution to communicate to the DisplayMetric() class.

I tested this on V69 and Android 2.3 and the sample below works:

save this as "testdpi.java"


The sample app:



some more information here: http://www.monkeycoder.co.nz/Community/posts.php?topic=4958