Is there a way to determnine if current device tablet in Android 2.2 and higher?
Currently I'm using minimal screen dimension, but there is some tablets (Viewsonic ViewPad7 480x800) with resolution like phones or even smaller (e. g. HTC Evo 3D has 960x540).
Is there any hardware property to read?
There is no reliable difference between phones and tablets, it is only possible to point it using specific features, the below can be used in conjunction with each other
The String Build.VERSION.RELEASE will give you the user-visible version string (i.e 1.5, 1.6, 2.0), while Build.VERSION.SDK_INT will give you a value from Build.VERSION_CODES. Remember Build.VERSION.SDK_INT works on Android 1.6 higher but not for 1.5. Build.VERSION.SDK will work on 1.5 or higher.
http://developer.android.com/reference/android/os/Build.html
http://developer.android.com/reference/android/os/Build.VERSION.html
And DisplayMetrics describes general information about a display
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
You can use getResources().getConfiguration()
For example:
getResources().getConfiguration().navigation
The following code will return the actual resolution of the physical device.
Display d = getWindowManager().getDefaultDisplay();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
int h = d.getWidth();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
int w = d.getWidth();
Related
I'm looking for something like this:
CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
cameraManager.setTorchMode("0", true);
cameraManager.setTorchLevel("0", 2); //change the brightness based on the number given
This will work only on devices with android 13 and new version of camera hardware abstraction layer (tested on pixel 6 pro). It might also work on some samsung devices with lower android version (not tested).
I'm a newbie in mobile development and I try to develop an Android application for several models of smartphones and tablets.
Now I'm experiencing some problems with selecting an appropriate layout parameters for Xiaomi Redmi 6a [https://www.gadgetsnow.com/mobile-phones/Xiaomi-Redmi-6A]
I designed a layout specially for it : land-xhdpi-1440x720
But when I try to run my app it seems to be, that it selects land-xhdpi-800x480.
Why so? What Am I doing wrong in this case?
And, by the way, could you recommend me some articles about layout selection for different types of devices based on some real experience (not an Android Developers Manual)?
It's considered bad practice. Why do you need layout for particular device?
Screen dimension for resource qualifier is set in dp, not in pixels. For xhdpi screen with 1440x720 px, screen size will be 720x360 dp
According to the layout folder spec it does not appear to allow specifying both screen dimensions in a layout folder name.
If you want to check the device model in your app you can use the Build class (the BRAND, MODEL and DEVICE fields) and select the appropriate layout at runtime by calling setContentView(R.layout.*) in your Activity/Fragment.
However, you should be aware that matching the device model to the screen resolution can be challenging, especially since on some phones the resolution can be changed in settings.
It would be safer to check the actual resolution at runtime:
DisplayManager mgr = (DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE)
Display[] allDisplays = mgr.getDisplays();
Display targetDisplay;
if (allDisplays != null && allDisplays.length > 0) {
//select the display to use, usually there is only one (or find the one with the highest resolution)
targetDisplay = allDisplays[0];
//get the resolution
Point sizePt = new Point();
targetDisplay.getRealSize(sizePt);
//or
DisplayMetrics metrics = new DisplayMetrics();
targetDisplay.getRealMetrics(metrics);
}
Also note the actual values may not be equal to the physical resolution due to the height of the notification and navigation bars so be sure to run some tests and try to use more general comparisons than
sizePt.x == 1440 && sizePt.y == 720
This is how I am getting the screen density using Android Studios
float density = getResources().getDisplayMetrics().density;
According to device specification, S8+ has 4.0 Density value and falls under xxxhdpi category
Refer this site https://material.io/devices/,
Have Attached a screenshot for reference
here
But the value returned by the above code is 2.9, which seems very wrong
check here
Tried using densityDpi also, but it also returns xxhdpi as opposed to xxxhdpi
int densityDpi = context.getResources().getDisplayMetrics().densityDpi;
The above code works well on other devices, tested on OnePlus 5T, Nexus 6P, Redmi Note 4, Moto g4+ and others...facing issues with the Galaxy s8+, haven't tested for Galaxy s8 but i guess the result will be same.
Is this a know Bug, or am I doing something wrong?
Its getting difficult to manage layout for S8+ devices without the correct pixel density info for s8+ devices.
Just like Surfman said. Your phone isn't at the maximum possible resolution. If you take the resolution reported within your screenshot and divide it by the reported 2.9 and multiply it by 4 you end up with roughly the maximum resolution of 2960x1440
You need to initialize display metrics object
You can try this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
flat density=metrics.density;
flat densityDpi=metrics.densityDpi;
I need different layout for device 4.0 , 4.5 and 5.0.
But I checked screen size width using:
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpWidth = outMetrics.widthPixels / density;
And on
Sony Xperia Sp I have W: 360.0 H: 592.0
Sony Xperia Z I have the same result W: 360.0 H: 592.0
but screens have differents sizes and layout looks really diffrent.
How to create different layouts for this and other devices sizes correctly?
EDIT:
In this case layouts look really different because one of phones had changed FONT SIZE in Device Settings.. GG Well played testers : )
Thx all. Up vote.
Android uses density independent pixels (dp), so it should be quite easy to adapt to different sizes. In your res folder create subfolders like layout-w320p and customise your layout inside that folder to adapt to different screen sizes.
Always try using RelativeLayout instead of FrameLayout and position items relative to the device. Item sizes should be made wrap_content so that it will automatically be shrinked to the device.
You can provide different layout based on exact width in pixels since you already know them:
Examples
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-w600dp/main_activity.xml # Multi-pane (any screen with 600dp available width or more)
Read the official android documentation about Supporting Multiple Screens
Sony Xperia Sp I have W: 360.0 H: 592.0Sony Xperia Sp I have W: 360.0 H: 592.0
Sony Xperia Z I have the same result W: 360.0 H: 592.0.
This is not screen size.. It totally depends on your device screen resolution.
If maintain this screen resolution you should create different layout or value file as
value-sw320dp-hdpi
value-sw320dp-xhdpi
value-sw320dp-xxhdpi
OR
drawable-sw320dp-hdpi
drawable-sw320dp-xhdpi
drawable-sw320dp-xxhdpi
I have made several values folder and dimen.xml files for configuring my app for different screen sizes. The preview in Android Studio shows them perfectly however, when I run the app on the phone, those dimensions don't take effect. And the layout shown in the Android Studio layout and the layout shown on the phone are different.
So for example on my nexus 5 the app should read dimensions from values-w640dp/dimen.xml however, it reads them from values/dimen.xml for some odd reason.
What can I do to resolve it?
Here are the screenshots.
Android Studio Preview Snapshot (Nexus 5)
Nexus 5 Phone Screenshot:
So dp are calculated using the following formula:
pixels / dp = dpi / 160dpi or pixels / dp = density
Looking at device metrics, the Nexus 5 has a density of 3 so:
dp = pixels / density
dp = 1920px / 3 = 640dp
dp = 1080px / 3 = 360dp
Using values-sw600dp would not fix it because that is not the actual smallest width of the screen since it can also be 360. So you should use values-sw300dp. Be warned though that this covers a very wide variety of devices. values-sw600dp usually targets tablets.
I have also run into issues where the true resolution of the device does not match what the emulator created, with the Nexus 5 too, so make sure you check the AVD screen and ensure that the resolution matches the real resolution of the device. Example, the Nexus 5 should be 1920x1080 and it is. If you ever encounter this issue, recreate the emulator or try a different one entirely.