In android there are many different types of screen Resolutions available. How to adjust the screen resolutions using java code?
There is no way to alter the screen resolution programmatically. AFAIK, you'd need to do a hardware level modification to change it.
Instead, you must write your app in a manner that it scales well across displays. Use multiple images for different sizes and densities, use dp units instead of px units. Make layouts for different screen sizes. All of this and more is explained in the online documentation here.
If you are asking about how to make your App adjust to the device resolution, you should read the screen resolution using code below and make your app fill the space as your wish.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
As there are 1000s of different Android devices, you just can't have a single line of code to do it automatically for you.
If you are speaking about changing the screen resolution like in PCs. LCD screens have native resolutions which cannot be altered. However, in PCs, the driver or the hardware behind the monitor change the resolution by considering consecutive pixels as single pixel. This option is added in high resolution monitors just to keep the compatibility with old video cards. In mobile phones, we do not have a need for doing this, so there is no such options. When LCD monitors replaced CRT, they had a need to work in different resolutions like CRT which can work on more than one native resolutions.
public int AR(int input)
{
final float scale = getResources().getDisplayMetrics().density;
return (int) (input * scale + 0.5f);
}
where, input is the value you want to use.
Example:
ImageView TitleImage = new ImageView(this);
TitleImage.setPadding(AR(10), AR(5), AR(2), AR(3));
NOW THE IMAGE PADDING SUITS ALL KINDS OF RESOLUTIONS.
Related
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.
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.
I have read all the pages I could find about support multiple screens in android, including
Supporting Multiple Screens
Providing Resources
Screen Sizes and Densities
And many others. But I still don't understand what resources I should provide for it to correctly position drawables(sprites) on a Canvas.
It's my first time making a game and I am currently a game Whack a Mole. I am confused about how to use the ldpi, mdpi, and hdpi, folders and how to properly position sprites to draw over a SurfaceView canvas.
I have a background image, resolution 480x800, so I added it to my hdpi folder. Then I have 120x150 sprites of moles, that I should position correctly on the holes for that background.
Currently I am using the following code to draw it:
canvas.drawBitmap(toDrawBitmap, draw_x, draw_y, null);
draw_x and draw_y are pixels that I found trying to place them correctly:
So far everything is fine, they are correctly placed in my hdpi, 480x800 screen. And android re scales them correctly on the different resolutions.
But when I try to use a different resolution screen, they are all drawn in wrong places, out of the holes, some of them are even out of the screen.
Please correct me if I am wrong but for what I've read these three resolutions are the most common in phones:
240x320 small - ldpi
320x480 normal - mdpi
480x800 normal - hdpi
My goal is to make the game work properly in those three kinds of screen. So my question is:
Can I calculate a draw_x and draw_y value, that will work on all of the devices? If not, how do i solve this problem?
Yes you can calculate it using device width and height.
final Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
final int width = display.getWidth();
final int height = display.getHeight();
From my understanding of the Android support for multiple screens Support, layouts are scaled for screen density and size, whereas images are scaled for screen density.
This ensures that images will appear the same size on different density screens. To overcome possible scaling issues, it is recommended to provide different versions of the images with following the 3:4:6:8 scaling ratio between the four generalized densities.
However, if I have a screen that is simply to display a photo with some text underneath, and I want to take advantage of the extra screen size in some mobiles, and therefore display the photo at a larger size from the user's point of view than on the smaller screens, then what is the best way of doing this?
Also wrt displaying images, is it best to use wrap_content or dp – what is the advantage of one over the other ?
Thanks very much in advance
P
In answer to your first question:
You probably need to specify a different drawable resource for every image you're going to do. Here is a link to the android developer's site that specifies methods of doing that. http://developer.android.com/guide/practices/screens_support.html
Second:
If you specify the dp, you may cause some image distortion, however, if you use wrap_content, it may not be big enough. I would suggest for you to use wrap_content with different drawables for each screen density.
EDIT:
To find the actual size:
Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight();
You're going to have to calculate it though based on the screen density.
And then just use mm or inches
All the different resolutions of the different Android products are driving me nuts.
My first android app that I wrote was designed so it supported the three commonly used resolutions: 240x320 (LDPI), 320x480 (MDPI) and 480x800 (HDPI). The 480x854 didn't do any harm to the layout because it has the same width as 480x800.
I've also bought the following devices to test my android apps on:
Samsung Galaxy Europe (LDPI)
HTC Desire Z (HDPI)
Luckily my girlfriend has a HTC Wildfire S (MDPI) so I've got most resolutions covered.
But today, my brother downloaded my app on his new HTC Sensation which has yet another resolution 540x960 (HDPI?). Which didn't show my app as it should and probably the most tablets won't show it correctly either.
What I've did with my first app was read out the density and then set the parameters:
public void set_ui_parameters() {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
if(metrics.densityDpi == DisplayMetrics.DENSITY_HIGH){
textSize = 35;
timeWidth = 80;
dayWidth = 110;
moneyWidth = 50;
} else if(metrics.densityDpi == DisplayMetrics.DENSITY_MEDIUM){
textSize = 35;
timeWidth = 53;
dayWidth = 73;
moneyWidth = 33;
} else if(metrics.densityDpi == DisplayMetrics.DENSITY_LOW){
textSize = 28;
timeWidth = 40;
dayWidth = 55;
moneyWidth = 25;
}
}
Besides the parameters I've also created drawables for LDPI, MDPI and HDPI. This works fine for the resolutions described above, but this depends on the screen resolution i.c.w. screen size and fails for, for example the HTC sensatoin with 540x960.
I know that not all the resolutions are used that often, but I would like to support as many as possible. Stats of Screen Sizes and Densities
I've read Supporting Multiple Screens multiple times but didn't found a clear answer to this "problem".
So should I read out the resolution and set the parameters according to the resolutions instead of density? Is this a smart thing to do or how do you cope with this?
Thanks a lot for your information!
You don't have to do that to support different densities. What you do is create different resources folders:
res/values-ldpi/dimens.xml
res/values-mdpi/dimens.xml
res/values-hdpi/dimens.xml
Then Android will decide which file to use. You can have something like:
<!-- in values-ldpi/dimens.xml -->
<dimen name="textSize">25dip</dimen>
and..
<!-- in values-mdpi/dimens.xml -->
<dimen name="textSize">20dip</dimen>
etc. And you shouldn't care about resolution... there are a lot of different resolutions sizes so it would be a hell to take decisions based on that.
Also, if you use dp instead of pixels, you hardly ever will have to create different dimensions files for each density. Of course, sometimes you have to, but it depends on the app.
Only thing you have to do is, that you set android:minSdkVersion to 7 or higher in your manifest file. Is is possible that some views will appear slightly different but app is
applicable and on whole screen.
Now, i'm just guessing here since the rest of the implementation isn't shown, but I'm assuming you are using those derived measurements as px
Take a look at dp. That essentially does all those things you did, automatically, for any device. (Device Independent Pixel)
Don't worry about the slight difference in resolution: 540x960 is only slightly bigger than 480x800. The extra vertical space is easy: it gives you more room for your lists. For horizontal space, as long as you're handling your layouts correctly you'll simply have extra padding (30pix ea) on the sides.
Unfortunately, full-width images (480 wide) are a slightly bigger problem. If you want pixel perfect images, you'll want to use scaleType="center" so the image centers but not scales. If you want full width, you can use scaleType="fitCenter" to make it fill. It will be a bit fuzzy... but so many thing on Android are ;-)
I ran into this problem as well, except my app is a game where there is one SurfaceView canvas that I draw background images and sprites to (similar to LunarLander example).
The way you handle it here is to extend your background image to the largest size you are willing to support (540x960) but you keep all the important things (like buttons, text, information, etc) within a smaller rectangle of 480x800. You can extend the image itself, or just add borders. The key is that nothing important is there.
People with 480x800 phones will see your normal app. People with 540x960 phones will see a little extra border.