Selecting a different layout directory based on screen Height - android

In my application, the layout must have a certain look but this is hard to achieve in many different screen resolutions and densities. I've read the developers article and tried my best to support many different screens by creating the following layout files :
However, 4.7" phones(1920x1080) and 5.8" phones(like my s8 : 1080x2220) use the same layout-sw360dp directory. Due to their difference in screen height resolution, the ui elements don't show up correctly for both phones. I want to know how i should go about solving this problem, can i use another qualifier to make android studio select the appropriate layout directory based on the phone's height or something? I'm starting to get lost here. Any help is welcome

After digging even deeper, i found out that you can choose different layouts inside the onCreate() method based on info from DisplayMetrics like so:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
if (...) {
setContentView(R.layout.ex1);
} else {
// .....
}
By using the height and width variables as the conditions inside the if statement , i managed to select the appropriate layout files for each screen size!

Related

Android layout design using values folder i.e. style or dimen

How to design proper layout or provide different margin,width and height for two different device i.e 3.2'' HVGA Slider (ADP1) (320X480;mdpi) and Nexus One (3.7'',480X800;hdpi) using values folder, because both devices access same value folder but look is not same.
According to doc both devices will really access the same folder.
I had similar issue some time ago and I used this code:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
and after you can make conditions like
if(width>=320 && width<480){
yourView.setTextSize(myTextSize); //or use LayoutParams to define more options
or use switch/case operator instead
This is how I solved it, but if you have many many view elements, could be not useful.

Android set View visibility programmatically based on screen size

I have a view in my android app that I would like to toggle between visible/gone on smaller screens, and visible/invisible in larger sizes. The initial set up (gone for small, invisible for large screens) is done by having two separate XML layout files under layout and layout-sw600dp-land, but then when I need to dynamically swap the visibility setting, how can I determine from within Java code which one to pick based on screen size?
Edit: more specifically, I want to detect in my code the same condition that causes Android to use layouts from layout-sw600dp-land. I was thinking even recording the value somewhere in the values-sw600dp-land directory, but not sure which file to put it into and how to access it.
You can get the size in pixels of the screen using the following.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
However, your question was ambiguous as to whether size of screen meant pixels or inches. You may need to take advantage of the dm.densityDpi value, to convert the values from pixels to inches, for a more useful calculation of the "size" of the screen.
ANSER FOR EDITS:
There are two potential solutions. One is referenced in this thread, very simple and you alluded to it above.
How to programatically determine which XML layout my Android apps is using?
The second isn't a solution, but rather an explanation. The layout-sw600dp-land file replaces an old naming convention pre 3.2 that went like this layout-xlarge-land. This essentially manes "xlarge" screen in "landscape" mode. So you can detect this programmatically by finding xlarge screen sizes, in which the width > height. Below is a good reference to compare the old convention vs the new "sw600dp" = smallest width is 600 dp convention.
http://developer.android.com/training/multiscreen/screensizes.html

Showing pictures in Android app - Resolution?

I've a Photo gallery in my app. The set of pictures are stored in the drawable folder. I'm making use of ViewPager when the user swipes through the images.. What is the width and height in pixels and dps for xhdpi, hdpi, mdpi??
I know that the Android documentation says px = dp*dpi/160.. but I'm still confused about what should be the pictures download pixels so that it fits in all screen sizes??
There isn't one magic size that guarantees that it fits all screens perfectly. There are more than one screen size that fall under each density.
You should look at making your layout scale well on as many devices as possible. In your case it sounds like you shouldn't be focusing on an a specific pixel size, but rather how to display correctly on the common screens and gracefully display on less common ones. That being said I would do the following:
I'd look at the dashboard here. I'd make layouts targeting first targeting hdpi with a normal screen size(50.1% of the market) followed by xhdpi(25.1%) with a normal screen size, followed by mdpi(11%) normal screen size. Check table 3 on this page for common screen sizes for those values. Since you will be most likely using an image view make sure to check out the scale type attribute to help handle when the image view isn't at
As a side note(maybe useful later)if you are having difficulties translating sizes between densities and raw pixel values use this calculator.
Use following function which give you height and width of current display
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
deviceHeightPix = displaymetrics.heightPixels;
deviceWidthPix = displaymetrics.widthPixels;
based on this, you can pass data on server to fetch relative images.

Supporting different screen size

I want to make game for supporting all Android device(Tablets and phones).I can get screen size through coding.
Do I have make 3 0r 4 layouts small , medium and large?
Which size for small , medium , large and extra large?
Is there any other way to set layout for all devices?
Here is the Android documentation for what you need:
http://developer.android.com/guide/topics/resources/providing-resources.html
You can use the screen-size, screen pixel density, or other things to determine how to separate your layouts.
You can specify different layouts (and you should try to). Different sizes may use the same layout. What layout the device ends up using depends on a set of rules.
So for example if you wanted to use screen size, you would want to make folders under your res folder called layout-small, layout-medium, layout-large, layout-xlarge. This is because layout is the default folder for layout resources, and then you add a -. In this case, small, medium, large, or xlarge.
Edit: Alex's link above might be what you're looking for. My link is more about HOW to do it, but Alex's link is more about how to do it WELL. I wasn't sure what you were asking exactly.
(1) One way to support all the layout sizes is to put images of suitable sizes into drawable-hdpi, drawable-mdpi, drawable-ldpi and drawable-xhdpi. The image for a device of a particular screen size will be automatically picked by Android runtime system. In this approach you will NOT have to make different layout files for each size.
(2) Second way to achieve the same is to dynamically detect the size(density) of the device and then set the layout accordingly, which can be done like this:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
switch(displayMetrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
// your layout for small-size devices.
break;
case DisplayMetrics.DENSITY_MEDIUM:
// your layout for medium-size devices.
break;
case DisplayMetrics.DENSITY_HIGH:
// your layout for big-size devices.
break;
}
In this approach, you MAY have to make different layout files for each size separately.

Layout Alignment Issues For Device to Device

When I try to run my project on LG Android Mobile then there is no alignment issues come with this device it is a 3.2 HVGA but when I try to run it on Motorola it is a 3.7 WVGA then it gives complete layout alignment issues so can you tell me suggestion to implement layouts uniquely to every device.
I don't know is it possible or not to make a unique layout design for all devices.
You can't create custom layouts for different devices, but you can for different screen densities and sizes, Supporting Multiple Screens has all of the information you should need.
You can create custom layouts by reading the device layout screen like this
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
There are two ways you can create your layout. You can create your own algorithm that checks the difference between the resolution you have made your layout for, and gets the difference for the current screen size, and adjust all those values progmatically (long and tedious, but DEFINETELY will work on all devices)
Or, you can define a layout for each of the common devices in your layout folder. This requires alot more space and time though.
Sometimes back I was having this type of problem. Because I was using Pixels as my unit to specify height, width or any layout specific dimensions. But I changed those px to dp and it worked for me. I got same layout for all the screens. Hope if this may help you anyways...
Here is a quick checklist about how you can ensure that your application displays properly on different screens:
1.Use wrap_content, fill_parent, or dp units when specifying dimensions in an XML layout file
2.Do not use hard coded pixel values in your application code
3.Do not use AbsoluteLayout (it's deprecated)
4.Supply alternative bitmap drawables for different screen densities

Categories

Resources