App to fit to other Screen sizes - android

I have finished creating an app but after taking a look on other phones I have seen that the content of the app does not fit or has moved on the screen from what I set it out as on Eclipse.
So I was looking around on StackOverflow to find out how to do this and I found this short code.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
switch(displayMetrics.densityDpi){
case DisplayMetrics.DENSITY_LOW:
// layout for small sized devices.
break;
case DisplayMetrics.DENSITY_MEDIUM:
// layout for medium-sized devices.
break;
case DisplayMetrics.DENSITY_HIGH:
// layout for big-sized devices.
break;
}
I am a little confused on where to put this and what to do, I would guess that this code would go into the pages XML files? and in the //layout for xxxxx would go the xml code for that size phone.
But how do I know how to space the app out without having lots of phone to test them on? just keep using the inbuilt emulator to test on the smallest screen size the medium and the large screen size till it all fits?

Create a single layout using WRAP_CONTENT attribute and use different type of images for all resolutions like drawable-mdpi,drawable-hdpi etc. the key is to use different drawables in proportion and let the OS calculate things and draw the layout for you.
Also refer to this answer.

Related

Selecting a different layout directory based on screen Height

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!

Adjusting to different screen sizes not working?

I thought I was successful in adjusting for different screen sizes(I was using the eclipse emulators and creating different screen sizes to test my app) but when I test my app on actual devices the result of my app varies. for example for a large screen size I set my emulator to a Nexus S and it will work and look fine, but then I try on an Alcatel One Touch Fierce(real device) which is still considered a large screen size the app play is just a little bit off, then I play it on another device which is also considered a large screen size the app will play just like the emulator. So i guess mt question is why? a samble of how I'm checking for different screen sizes is below:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
switch(displayMetrics.densityDpi)
{
case DisplayMetrics.DENSITY_LOW:
// layout for small sized devices.
break;
case DisplayMetrics.DENSITY_MEDIUM:
// layout for medium-sized devices.
break;
case DisplayMetrics.DENSITY_HIGH:
// layout for large devices.
break;
case DisplayMetrics.DENSITY_XHIGH:
// layout for really large devices.
break;
Before your case statement, try to print the density value like this:
Log.i("Sushil", "displaymetrics.densityDpi : " + displaymetrics.densityDpi);
And check if it matches with any of your defined case statement. Else add new case statements, it should work. Few more defined cases are :
DisplayMetrics.DENSITY_TV
DisplayMetrics.DENSITY_XXHIGH
Hope this helps.
i don't see any thing wrong with the outcomes. if you run an app designed on a phone emulator-----on a tablet, the layouts will not match.
you have to decide if you wanna support different screen sizes or not if you do, then you'd have to create diffrent layouts for different screen sizes and set the corresponding layout in the OnCreate method of your Activity.
here is how to check if the device is a tablet or a phone:
if(isTablet==true){
setContentView(R.Layout.my_tablet_layout);
}else{
setContentView(R.Layout.my_phone_layout);
}
public boolean IsTablet() {
return (getApplicationContext().getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

Android Button Sizing

I have several buttons in my application that are displayed at the bottom of the screen. Right now the buttons have text in them. When running on the emulator, the buttons with text fit nicely. Now, that I am running on the actual device, some buttons' text takes more than two lines and the screen is not very presentable. I could change the font to make it work for the device in question, but there is no guarantee that it will work on some other device. Should I create button images (with text embedded as part of the image) and then have multiple versions, depending on the size of the device screen being used? That seems like a lot of work, is there a simpler solution to this?
Thank You,
Gary
You need to give equal weights to all buttons.So that all of them look similar and occupy same amount of space.
You have to get screen resolution and set sizes as a proportion of this resolution.
Here is the sample code to obtain screen width and height.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
It is not hard but little bit tricky.
In this purpose you can use built in draw-able folder. In android project there are many draw-able folder like drawable-hdpi, drawable-mdpi, drawable-xhdpi where you can put different size of images and it will automatically render image based on device screen. Check this tutorial for more understanding Supporting Multiple Screens
Or you can take screen size dynamically. Based on the screen size you can set the button height and width.
You can find multiple screen size handling tutorial here:
Supporting Multiple Screens
your emulator may have specific resolution that is different than the one of your actual device.

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.

Categories

Resources