Wrong layout is selected for a screen density - android

I have three layouts :
/layout/layout.xml
/layout-xhdpi/layout.xml
/layout-xxhdpi/layout.xml
And i have two emulators :
Nexus 5x with Api level 28 screen size 1080x1920 420dpi
Pixel 3xl with Api 28 screen size 1440x2960 560dpi
What do I expect?
Nexux 5x should select the layout in /layout-xhdpi/layout.xml
Pixel 3xl should select layout in /layout-xxhdpi/layout.xml
What happens instead?
Both devices select layout in layout-xxhdpi.
I have also tried to replace by folder such as layout-sw420dp and layout-sw560dp one or both emulator select another layout.
Why does this happen, is there something?

Considering you are running one project on two devices you must go as per following approch:
set the setContentView(R.layout-xhdpi.layout.xml); in java
then tun the project on Nexux 5x
now set the setContentView(R.layout-xxhdpi.layout.xml); in java
then run the project on Pixel 3xl
If anything concerns you please leave a comment

First get the screen size of device by using:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
After this you can use if else statement like
if(height==first phone display height)
{set the setContentView(R.layout-xhdpi.layout.xml);}
Else{setContentView(R.layout-xxhdpi.layout.xml);
}
Remember you need to pass exect screen size at place of first phone screen height

Related

Android-Studio: Layout for wide screen in a portrait mode

I have an application that should work in a portrait mode only.
it works great using just (Layout) resources.
Now I face a problem with some device,
The device has only Portrait mode, no orientation, it is ok for me.
The screen height is smaller than its width, and remember this is not landscape mode.
__________
| portrait |
|__________|
If I used Layout-land, it will not work, and the device pick the normal layout resource.
I tried layout-sw, layout-w, layout-h, without solution.
I want the normal devices to use "layout" and the devices which has height smaller than the width use another layout.
And remember please, this is not a landscape.
Thanks in advance.
===========================================================
Update
Is there any way to use Layout-11x00, where 11 is the width, and the 00 is the height? I tried it and it worked, but the problem is it worked for all devices, not the wide devices only. I tried Layout-774x480 as w=774 and h=480. but it worked also with my Samsung mobile, I do not want this.
Why when I set the layout to "Layout-774x480" it targeted my samsung? while my samsung width is not > height?! I mean what is the different between "Layout-774x480" and "Layout-480x774"?
You can set a qualifier along with the layout folder name to get your layout picked from that specific folder when you are running your application in a device. Here is the documentation where I recommend you to take a look.
In your case you just have to create a layout directory named res/layout-w600dp/main_activity.xml which indicates if the device screen is 600dp wide and bigger, the layout will be picked from this folder.
I checked that you have tried already with layout-w, layout-h and layout-sw settings. However, I think you are missing the expected width of the device screen which should be picked.
Hope that helps!
Update
If you are willing to set different layout based on the decision that the height is less than the width, then you might just consider checking the screen height and width before setting the content view of the activity.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
if(height > width) setContentView(R.layout.main_activity.xml);
else setContentView(R.layout.main_activity_wide.xml);

Android Studio: Flexible Gridlayout

I started to create an app with the gridlayout. After I wrote the XML-knote of the gridlayout I placed within the gridlayout several buttons.
But if I change the Android Device from Nexus N to Nexus S the Layout starts to look like this:
and the same problem with the size goes on with Nexus XL
What should I do to keep the layout size of Nexus N to all the other devices?
DisplayMetrics displayMetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels /NumberOfButtons;
width=displayMetrics.widthPixels/3;
You need to set the button height dynamaically at the runtime
Through above code you can get height and width of screen.

Differentiate tablet with phone in manifest

Is it possible to differentiate tablet with a phone in manifest: I would like to say if tablet open tablet.class Java file or else open phone.class as the main screen.
I know I can do a setcontentview according to the tablet or phone but the problem is that the layout names and other things are different in both phone and tablet.
For example:
TabletActivity.xml
Layout 1 and Layout 2 with two buttons.
But for PhoneActivity.xml
Layout 1, Layout 2, Layout 3, Layout 4 with 4 buttons.
So If I use same JAVA file - I will end up having long code with conditions - instead if I could differentiate this in the beginning like If tablet just open tablet.class if not open phone.class - It would be easier for me?
AGAIN: I am able to differentiate tablet with phone in my mainactivity.java file. I try ing to know will I be able to do it in manifest reason because I have 20 different theme so I have 20 different xml file for tablet and phone and one java file (right now, Oncreate of my mainactivity - before setcontent I am checking if phone or tablet and then theme value and setting the content accordingly). I am wondering is there any easier way than this method???
Or shall I use two different APK's that works only for phone or tablet?
Is this possible?
Let me know!
Thanks!
Try this code. You can get the screen inches
String inputSystem;
inputSystem = android.os.Build.ID;
Log.d("hai",inputSystem);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Log.d("hai",width+"");
Log.d("hai",height+"");
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(width/dm.xdpi,2);
double y = Math.pow(height/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
Log.d("hai","Screen inches : " + screenInches+"");
after that you just decide what is tablet and what is phone by screenInches
Screen inches alone won't do it. Lots of devices are in the "muddy" middle ground and could be called "phablet".
We use layouts to set our device. We use res/layout-sw600dp and res/layout-sw720dp folders. they correspond to devices of the smallest width of 600 or 720 display pixels which makes a nice determinant for 7" or 10" tablet sizes
You will be better served by following the Android guidelines for determining the device size with this link
http://developer.android.com/guide/practices/screens-distribution.html

Can you specify which resource folder an Android device uses?

I just got the new Nexus 7 which pulls images from the drawable-xhdpi folder because it is 1920x1200. The Nexus 10 also pulls from the drawable-xhdpi folder. The same image looks too big on the Nexus 7 as a result (too big by 150% to be exact). Is there any way to specify a resourse (drawable) folder to use for a specific device?
It sounds like I should not be trying to over-ride the default drawable buckets since there are so many screen sizes and it could hinder me when I attempt to support even more devices. So then what is the best way to handle this problem? The below images illustrates my problem exactly. The play button gets scaled down from the Nexus 10 to the new Nexus 7 while the bearded guy gets scaled up so that it remains as the same physical size on both devices. My images are currently doing what the bearded guy image is doing but I would like it to do what the play/connect buttons do which is scale down on the New Nexus 7.
(top image = New Nexus 7
bottom image = Nexus 10)
In order to keep using the drawable resource folders, I had to do the following to differentiate the Nexus 7 2013 and the Nexus 10 since they both would use the xhdpi folder by default:
For the Nexus 7 I ended up using: drawable-sw600dp-xhdpi
For the Nexus 10 I ended up using: drawable-sw720dp-xhdpi
I had 2 sets of assets scaled differently in each folder.
You can't set a drawable folder for a specific device, and it's also impractical, since there are too many devices out there (see here for example).
However, if you are unhappy with the way android choose the buckets of which file to use for each device configuration, you can use your own algorithm. there are 2 options to where to put the files in this case:
put the image files in res/drawable-nodpi (or res/raw) . for each image add a suffix/prefix to show which configuration it's used for.
put the image files in assets . this is a more flexible method, since you can even create sub folders and any file name you wish.
in both methods, you should also use your own image-sampling method , depending on your own rules.
You can use the following code to get the size and then scale the image to the required size based on the size of the display.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenDensity = metrics.densityDpi;

Android Emulator actual screen size

I want to optimize my app. I have a user with a Samsung Galaxy Note, 1280x800, 320dpi, Large Screen.
How do I set up the emulator's actual inch-screen size?
If I choose WXGA800 with 320dpi it makes a normal sized Version. How do I make it large?
Edit. Thanks for your answers, but that isn't the point. I know how to set up a an Emulator screen resolution and dpi. Actually I want an emulator to edit "layout-large-xhdpi" layouts but I get a "layout-normal-xhdpi" using 1280x800 with 320dpi in the AVD Manager. I simply want to change xlarge to large. Is there a way?
Edit2 Okay,found a workaround by temporarily removing the layout for normal sized xdhpi devices(Galaxy Nexus) and editing the Galaxy Note Layout as if it were some kind of "xhdpi" layout. But still it is strange to me, that it is impossible to edit directly a specific folder file like "layout-large-xhdpi". Found only in the Layout-Editor the possibility to add a screen size but unfortunately it won't make my layout.
Nevertheless I will have to make my layouts more flexible to have less problems ;-)
Thanks anyway
You can simply do that by opening your AVD Manager from Eclipse and when you are creating the emulator just check the Resolution radio button and input your desired screen size. Don't forget to change the density of the screen too. If you just want to emulate a Tablet you can simply make an emulator with Android API 2.3 or higher and it should have the desired dimensions..
#le chiffre : may I know why you want to increase the screen, as from my experience, if you use Eclipse then I think you can't increase the screen resolution of Emulator (if I'm wrong correct me please). The size of the emulator in landscape mode: width:800 and height:400 and I get this by this code
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
and if you want to scale your application, pictures,etc., you need to get the device width and height, so you don't need to worry about screen resolution. But I don't think emulator size can be increased.
Thx and hope this helps!
In Eclipse when you start the avd, you can set the option scale display to real size and set the display size and dpi.
Window -> Android sdk and avd Manager -> start -> you will get option here

Categories

Resources