i have developed the android application screen resolution is 320x480 Px but i want to run same application without any code modification with Droid Mobile(480x854 px).
i have installed android application with droid mobile but it's displaying only half of the page in droid mobile( i am using the android 2.0 SDK device).
is there any way to resolve this kind of issues?
Regards,
Jeyavel N
Yes: Use density independent pixels (dip) instead of pixels (px) when specifying dimension and position of screen elements. That way, Android will automatically scale these values to different screen resolutions and pixel densities.
When having to specify these programatically, you may find this conversion method useful:
public static int dipToPx(Activity context, int dip) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (int) (dip * displayMetrics.density + 0.5f);
}
By just following that, and a little fine tuning, I was able to get our app running on all different screen sizes out there.
Related
I have been tasked to complete an Andoid app alongside our existing IOS app.
The project skeleton for Android is already there I just need to fill in the gaps to bring it up to speed with the IOS version.
I have been testing via my own phone and have been struggling with fonts being too large on my Galaxy S6.
Now I have just realised that this is due to the actual Zomm and Font display settings on my device (my eyesite isn't the best).
My question is, is it possible to retrieve these zoom settings from my device so that I can adjust font's etc accordingly?
Pretty new to Xamarin but I'm getting there - hopefully you'll have some pointers to help me get further.
As we all know, the conversion formula from dp and px: px = dp * density
It can be seen that if the design width is 360dp, we can only modify the value of density if we want to ensure that the px values calculated by all devices are exactly the width of the screen.
By reading the source code and official document, we can see that the density is a member variable in DisplayMetrics, and the DisplayMetrics instance is available through Resources#getDisplayMetrics, and the Resouces is obtained through the Activity or Application Context.
We can familiarize ourselves with the following variables related to DisplayMetrics neutralization adaptation:
DisplayMetrics#density is the above density
DisplayMetrics#densityDpi is the above dpi
DisplayMetrics#scaledDensity The scaling factor of the font, which is
equal to the density under normal conditions, but will change this
value after adjusting the system font size.
Solution:
The following assumes that the design map width is 360dp and is adapted to the wide dimension.
Then the adapted density = device real width (unit px) / 360, then we only need to modify our calculated density in the system, the code is implemented as follows:
private static void setCustomDensity( Activity activity,Application application)
{
DisplayMetrics appDisplaymetrics = application.Resources.DisplayMetrics;
float targetDensity = appDisplaymetrics.WidthPixels / 360;
int targetDensityDpi = (int)(160 * targetDensity);
appDisplaymetrics.Density = appDisplaymetrics.ScaledDensity = targetDensity;
appDisplaymetrics.DensityDpi = (Android.Util.DisplayMetricsDensity)targetDensityDpi;
DisplayMetrics activityDisplayMetrics = activity.Resources.DisplayMetrics;
activityDisplayMetrics.Density = activityDisplayMetrics.ScaledDensity = targetDensity;
activityDisplayMetrics.DensityDpi = (Android.Util.DisplayMetricsDensity)targetDensityDpi;
}
Also called in the Activity#onCreate method. The code is relatively simple, and does not involve the call of the system non-public api, so theoretically it will not affect the stability of the app.
Note: If you switch fonts in the system settings and then return to the app, the fonts have not changed. So you have to listen to the font switch, call Application#registerComponentCallbacks to register the onConfigurationChanged listener.
I would like to know how the -webkit-device-pixel-ratio is calculated. I had already read this. Yet I am unable to understand it clearly.
I would also like to know if there is a list of which devices use which pixel ratio. The android website says
The Android Browser and WebView support a CSS media feature that allows you to create styles for specific screen densities—the -webkit-device-pixel-ratio CSS media feature. The value you apply to this feature should be either "0.75", "1", or "1.5", to indicate that the styles are for devices with low density, medium density, or high density screens, respectively.
but I found that we need to use -webkit-device-pixel-ratio=2 to make a web application compatible on 768 x 1280 resolution screen.
According to this article
http://www.html5rocks.com/en/mobile/high-dpi/
The magic number seems to be 150.
Dividing the physical ppi by the ideal ppi of 150, gives the device pixel ratio.
E.g. a device with 445ppi would have a dpr of 3 ->> 445 / 150
The simple calculation seems to hold up okay for many items on this list,
http://en.wikipedia.org/wiki/List_of_displays_by_pixel_density
You can find many of your screen parameters by visiting https://www.mydevice.io/
you can find the pixel ratio using javascript window.devicePixelRatio
Device Pixel Ratio (DPR) is the relationship between physical hardware-based pixels and Device-Independent Pixels which are an abstraction. The value of the device pixel ratio is not something which can reliably be calculated as it's based on how the device manufacturer intends to map one type of pixel system to the other. To reliably obtain this information, you will need to request it from the device.
The problem is that there is no one database with all of (even the most common) devices listed, so looking up the viewport specs for a given device are a case of trying to find the device in any one of several databases.
These are the databases that I've come across and used, that list device viewports along with their pixel ratios:
https://yesviz.com/devices.php (currently the most comprehensive)
http://dpi.lv/
https://www.mydevice.io/#compare-devices
https://viewportsizer.com/devices/
http://screensiz.es
Feel free to add to the list above, as I'm sure there are much more comprehensive databases out there.
Also remember that resolution / DPR = viewport, so for testing the responsiveness of a site you generally only need one of either DPR or viewport, and the viewport itself is generally better.
claculate display metrics and get
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi
and use dm.xdpi, or dm.ydpi i.e pixcel desity
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.
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();
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.