Android, get screen size in api 3.2 - android

In API 3.2 when you get the screen height, it seems that return value is the screen height minus the height menu bar.
Here is the code:
((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
int height=metrics.heightPixels;
For API 3.2 emulator with 1280x800 screen size, the height return is 752
I tried to get the height with methods but always return 752
Ok I get the answer. In API 3.2 you need to get height and width in onSurfaceChanged method. If you need to get the real values you need to get height and width in portait and landscape mode and select the non reduced measure in both modes.

Related

How to scale layout proportionally to screen's physical size?

Need the whole layout width to fit exactly the same within the portrait physical width from the screen in any device, so that it doesn't matter the DPI, the screen size, the pixel portrait width, nor the width-height relation.
All I have managed to do right now is to override the font size from the user's Android configuration, by adding the next code to MainActivity.cs:
public override Resources Resources
{
get
{
Configuration config = base.Resources.Configuration;
if (config == null)
{
config = new Configuration();
}
config.FontScale = 1f;
return CreateConfigurationContext(config).Resources;
}
}
That made a great improvement, but still see plenty of difference between different devices, those devices may have longer or shorter screens, greater or smaller DPI, and different sizes. For example device Pixel 2 from emulator, which has 1080x1920 resolution and 420 dpi, the layout fits perfectly (I made it that way), but when I try in physical device Redmi 9 1080x2340 resolution and 395 dpi (about 3 mm bigger portrait width), the layout becomes a little bigger, and elements don't fit in.
How to make the layout width to scale proportionally to the physical size, so that it can always fit within the width from portrait mode no matter the device?
You can try using percentage or ratio dimension attributes of ConstraintLayout.
ConstraintLayout : ConstraintLayout

Android-Studio: Layout for wide screen in a portrait mode

I have an application that should work in a portrait mode only.
it works great using just (Layout) resources.
Now I face a problem with some device,
The device has only Portrait mode, no orientation, it is ok for me.
The screen height is smaller than its width, and remember this is not landscape mode.
__________
| portrait |
|__________|
If I used Layout-land, it will not work, and the device pick the normal layout resource.
I tried layout-sw, layout-w, layout-h, without solution.
I want the normal devices to use "layout" and the devices which has height smaller than the width use another layout.
And remember please, this is not a landscape.
Thanks in advance.
===========================================================
Update
Is there any way to use Layout-11x00, where 11 is the width, and the 00 is the height? I tried it and it worked, but the problem is it worked for all devices, not the wide devices only. I tried Layout-774x480 as w=774 and h=480. but it worked also with my Samsung mobile, I do not want this.
Why when I set the layout to "Layout-774x480" it targeted my samsung? while my samsung width is not > height?! I mean what is the different between "Layout-774x480" and "Layout-480x774"?
You can set a qualifier along with the layout folder name to get your layout picked from that specific folder when you are running your application in a device. Here is the documentation where I recommend you to take a look.
In your case you just have to create a layout directory named res/layout-w600dp/main_activity.xml which indicates if the device screen is 600dp wide and bigger, the layout will be picked from this folder.
I checked that you have tried already with layout-w, layout-h and layout-sw settings. However, I think you are missing the expected width of the device screen which should be picked.
Hope that helps!
Update
If you are willing to set different layout based on the decision that the height is less than the width, then you might just consider checking the screen height and width before setting the content view of the activity.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
if(height > width) setContentView(R.layout.main_activity.xml);
else setContentView(R.layout.main_activity_wide.xml);

Image don't fit perfectly in layout

I've read pages over pages, but i still have problems with layout for multiple screens.
I have an image 1080 * 300 pixels that occupy nexus 5 screen horizontally, and is aligned on top of layout (a sort of header). Now if i scale it for xhdpi screens (like galaxy nexus and nexus 4), i don't obtain the same result. by follow scale unit, my 1080 px width image in xxhdpi becomes 720 px image. This dimension fit perfectly for galaxy nexus screen (it's gnexus screen width), but not for nexus 4 that is bigger (768 px), so image don't fill all screen width and i have a blank space on his right.
If i try to scale image to 768 px width, it fit for nexus 4 but not for gnex.
I've noticed also when i change virtual device for preview, this error from adt console:
Displaying it with ', , Locale Language ___Region __, Left To Right, sw384dp, w384dp, h640dp, Normal Screen, Short screen aspect ratio, Portrait Orientation, Normal, Day time, X-High Density, Finger-based touchscreen, Soft keyboard, No keyboard, Hidden navigation, No navigation, Screen resolution 1280x768, API Level 19'
What's wrong?
You can use a custom view called ScaleImageView which is written by Maurycy Wojtowicz.
Class is defined like below:
This view will auto determine the width or height by determining if
the height or width is set(exact size or match_parent) and scale the
other dimension depending on the images dimension This view also
contains an ImageChangeListener which calls changed(boolean isEmpty)
once a change has been made to the ImageView
Here is how you are going to implement it.
Create a class named ScaleImageView.java and copy contents of the link above.
In your xml file, create a ScaleImageView, just same like ImageView (the example I am writing below is for filling screenwidth, and scaling height according to that so there will be no empty spaces on right/left)
<com.project.customview.ScaleImageView
android:id="#+id/scaleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="#drawable/file" />
If you need to declare and set programmatically in your Activity, it is also the same as using ImageView:
imageView = (ScaleImageView)findViewById(R.id.scaleImageView);
imageView.setImageResource(R.drawable.file);

Orientation inverted on display with bigger width than height

I have a device on which I set portrait orientation, but it shows the activity in landscape. The device width is bigger than its height, so it seems to be inverted. What can I do to have this device working like the others?
Note that this device has a keyboard; see this image.

Device width for an android phone in landscape mode?

I'm looking to adjust my page width when an android user rotates their phone to landscape. Problem is, I don't know what the device width is to set an exact pixel amount for my container div. I guess at 640, but it doesn't seem right. Does anyone know, offhand, what the device width for a landscape-oriented android device is? Or does that vary from device to device?
Thanks!
You shouldn't be guessing for an exact pixel width. I will just use dip (instead of px) on some of the glue pieces (like padding or the sides) and just let the main application expand naturally to fill it up.
It varies from device to device. You have to make generic ways to draw the screens. If you're setting xml layouts, then you can set widths and heights using the various notations.
What is the difference between "px", "dp", "dip" and "sp" on Android?
If you're setting the size dynamically at runtime, then screen size can be obtained through the "Display" class.
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Log.d(TAG, "Height = " + display.getHeight() + " Width = " + display.getWidth());
The width and height will be adjusted depending on your orientation of the phone.
The information is generally given to you through the onDraw methods through the Canvas object. You can use canvas.getWidth() to get the width of the screen and canvas.getHeight() to get the height of the screen.

Categories

Resources