I know there are a lot of questions similar to my title but I couldn't find the answer that I want.
In my app, I am using window.innerWidth and window.innerHeight to get the screen size, but these two values return different size. For example when I run my app on Note 3, I get 360x615 while it should be 1080x1920.
The following code in Java gives me what I want:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
So is there any way in Phonegap to get the same screen size value as Java?
Update: when I test the same code on Note 2 with Jelly Beans I get the correct size 720x1230
I am using this code
hope it help you
var physicalScreenWidth = window.screen.width * window.devicePixelRatio;
var physicalScreenHeight = window.screen.height * window.devicePixelRatio;
I am assuming you want to figure out the actual device pixels instead of the more commonly measured CSS pizels? You could read up on this here.
Basically, if you want to know actual device pixels you could use screen.width and screen.height. If you want to know CSS pixels you could use window.innerWidth and window.innerHeight.
The meta viewport tag may be ignored on certain Android devices, those are my experiences with Samsung Galaxy tab devices anyway.
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 have a android tablet( Samsung gt-P1010 ) whose screen details is as follows,
Screen resolution- 600*1024
Density- 240
Screen size- Normal
I created an emulator of the same details like above but when I print the Width and Height, it gives 600*961 .Why there is such a big difference ? I am using the following code to get the device details,
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
screendensity = displaymetrics.densityDpi;
In my application I am trying to run a .gif image above the Alphabets for which I am taking margins based on the width and height of the device. Due to the above difference, emulator and real device gives different result. Please anyone tell me whats wrong here.Any help will be appreciated.
Display metrics give the screen dimensions including the status bar and excluding the soft keys area. On 2.x devices it will give you the complete screen size but on 4.x devices(which have soft keys) it will exclude the soft key area.
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.
Is there an API in Android that returns the full screen height of the device? I'm interested in the full height as specified in the device spec, not the height of the viewable screen as returned by
android.view.Display.getHeight();
I spent some time looking for this and didn't find anything like what you're asking for. Part of the problem is that the bar that takes up the pixels they're not including can potentially be of different sizes.
What I ended up doing is measuring the width of the device (which is the full width) and the not-quite-full-height to match against the standard resolutions with an approximate. It's not great. If you really need it exact, you can force an orientation change to match both dimensions exactly. That's pretty gross, though.
The real answer is that you're not supposed to care about the exact dimensions of the hardware and design using the OS-provided size buckets.
Does this not work for you?
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayHeight = dm.heightPixels;
For me, displayHeight returns 960 pixels on my HTC Sensation
Try heightPixels on a DisplayMetrics object.
I need to know how to set the LayoutParam according to the resolution of the mobile phones..
Im using a GridView where I'm using GridView.LayoutParams(45, 45).. Now if I'm using mobile with small screen size is ok.. But if I test with the big screen device like HTC Desire HD then its looking so small.. How to make everything as similar?
Does using android:numColumns or setNumColumns(int numColumns) not work for your needs?
http://developer.android.com/reference/android/widget/GridView.html#setNumColumns(int)
Divide your display's width in 3 parts (for 3 columns or 4 for 4 columns....)
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
imageView.setLayoutParams(new GridView.LayoutParams(width/3 , width/3));