Landscape Window Dimension Android - android

I have been having trouble getting the screen dimensions for android. I do not want the size of the status bars included in my dimenions. The app that I'm developing is to be permanently in landscape mode. I have been testing with the android honeycomb emulator. When I get the dimensions using the code in How can I get android Honeycomb system's screen width and height? I get 1280x800. I was curious as to why the person in the linked question has the size of the status bars subtracted while mine does not work that way. Also, how can I make it so that the size of the status bars is actually subtracted? If any additional details are needed I will be checking periodically to provide. Thanks!

Okay! I finally figured it out. I pieced together code from several different sources so I don't know exactly why things work out the way they do. Here is the relevant code.
Rect rect = new Rect();
Window win = getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(rect);
screenWidth = Math.abs(rect.left-rect.right);
screenHeight = Math.abs(rect.top-rect.bottom);
There is one caveat though. This code WILL NOT work in the onCreate() method. Instead, place it in
public void onWindowFocusChanged (boolean hasFocus)
This should solve any problems with getting window dimensions!
EDIT: I believe this solution should also work in portrait mode as well. I have not tested it though.

Related

How can the window scale change suddenly, while the screen scale remains the same?

When I switch focus to my React Native app, the window scale (found with Dimensions.get('window')) and PixelRatio sometimes change. The screen scale remains the same.
This messes up the layout of my app big time. Everything is twice as big or small. When not everything is rerendered I see a mix of different PixelRatios.
How can I solve this problem? How can the window scale be different from the screen scale in the first place and is there a way to prevent this?
Only thing in the documentation I could find about screen vs. window is the usable area.
Any help is appreciated, because I don't know where to look anymore.

How can we get the size of screen with notch on the top

There are some devices such as Huawei p20(running android 8,8.1) which has in-built notch on the top.
The current way to get the size of the screen is
Display display = getWindow().getWindowManager().getDefaultDisplay();
Point screenSize = new Point();
display.getRealSize(screenSize);
//screenSize.x
//screenSize.y
And it's leading the game out of screen. So, is there any way to solve notch issue with android P and Pre-p devices.
The status bar(where all your notifications are) always adjusts to the depth of the notch. Look at the pixel 3 which has the largest notch in the world :p The status bar becomes as tall as the notch.
So the notch is only a problem if your app is in a full screen(content draws underneath the status bar) and it may cover some of your content.
Make your app non-full screen (only draw below the status bar) and you don't have to worry about any notch size in the future. A lot of games ignore the status bar from their content.
There are only a few android devices with notch option and currently it is not possible to get the exact screen size of such devices using Android APIs.
BUT there is a work around, you can check the name of device and adjust your layout accordingly.
There is one popular Android library to get the market name of an Android device. Check more information here : https://github.com/jaredrummler/AndroidDeviceNames
How to use this lib:
String deviceName = DeviceName.getDeviceName();
Hope this will help you to focus on your development instead of wasting time!

Set brightness below the system limit using alpha and save value

I've been trying to set the brightness under the limit of Android settings. There are two similar apps in Google Play doing this:
Shades
Screen Filter
The second one, even allows to use the phone with a black screen, just what I need in my app.
There are a lot of other questions about brightness but not to set it under this system limit, and I've found one question asking almost the same but not helping me:
Brightness Screen Filter
So after a lot of search I've got following code:
int brightness=0; //0 to 255, so I set it to a low level (not enough for me)
ContentResolver cResolver = getContentResolver();
//set the system brightness value, and if active, disable auto mode.
System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
System.putInt(cResolver, System.SCREEN_BRIGHTNESS_MODE, System.SCREEN_BRIGHTNESS_MODE_MANUAL);
//previous lines don't set the brightness to the current screen so we set it like this:
LayoutParams layoutpars = getWindow().getAttributes();
layoutpars.screenBrightness = brightness / (float)255;
//Now we have brightness to the minimum allowed by Android but
//to achieve what these apps do, I have to reduce alpha of the window to the min
//then the screen is black like I want, and totally usable!
layoutpars.alpha=0.05f;
window.setAttributes(layoutpars);
Here the alpha is the key, this is doing what I want, but only for current window/screen, and I need to save to the system this alpha as default, like I'm doing with the brightness and the Manual mode for applying everywhere.
Do you know how I should do this? I'm unable to find a value "System.SCREEN_ALPHA" to set it with System.putInt or another way to do this.
In the other question I linked before, pheelicks replied with a suggestion of using a transparent non-touchable over screen, but this is not working for me, and the result does not give the feeling of a turned off screen like previous apps.
Thanks.
I decided to implemented this feature just inside my app with alpha. So I couldn't solve the initial question at all....
Anyways, in the end seems that the solution to this question is the one Pheelicks replied here: https://stackoverflow.com/a/4287597/1667990
-This is launching an activity to be always on top,
-with alpha to 0.0f so it be transparent,
-that redirects the touch to the activity behind:
//Let touches go through to apps/activities underneath.
Window window = activity.getWindow();
window.addFlags(FLAG_NOT_TOUCHABLE);
-And most important thing not explained in the previous link, is to set the dim behind our window to 1 (like the dim in a messagebox, but setting to the max to be black behind :D )
window.setDimAmount ((float)1.0) ;
window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
I haven't tried that and decided not to do it in my app as it could bring unexpected behavior in my circumstances, but I think it should definitely work, if anyone try please add your feedback!

Why can't i just calculate the screen dimensions and by that resize the image?

I found a lot questions and answers of people asking how to adjust the image to fit the device screen and i thought to myself- if i can get the device resolution, using
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
why can't i just scale/resize the image by lets say 1/5 of the screen?
Thats way i can do lets say 1/5 of space 2/5 of image and another 1/5 of text and its supposed to fit all the device's, Am i missing something here?
also why do i get a "deprecated" warning in this code?
Sorry for my english and thanks in advance for the help
In Android your application may not cover entire screen. Even if you disable titlebar/statusbar etc you can still have device-specific chrome (like on screen button in Android 3.0).
why can't i just scale/resize the image by lets say 1/5 of the screen?
You can, and if you want to have your image take up some relative amount of the screen size, then this and using a LinearLayout with weights (as pointed out by others) are probably your only options. Do note that you probably don't want to do any image scaling on the main UI thread and that you should try doing it as efficiently as possible. Have a look at the guidelines for some good practices.
Also note that the screen width (and especially the screen height) isn't necessarily the amount of space available for your image and/or UI. There may be margins/padding enforced, or other components present; for instance the ActionBar.
also why do i get a "deprecated" warning in this code?
See the documentation for either Display.getHeight() or Display.getWidth():
This method was deprecated in API level 13. Use getSize(Point) instead.
Basically Google has told developers it may stop supporting the aforementioned methods in future platform versions. Hence, you should migrate away from them when possible. Most developers will end up writing their own helper method that uses the deprecated approach on older platforms (pre API level 13 in this case) and getSize(...) on newer platforms. You can use the Build.VERSION_CODES for this purpose.

Android: correct way to get screen dimensions for target orientation?

My application is bitmap intensive, with pixel-exact layout (it's a sort of game, actually, and it's pretty hard to avoid this pixel-based coordinates).
What I wanted to do is to perform some layout calculations and bitmap pre-scaling in my onCrete - I use well known API - getWindowManager().getDefaultDisplay().getSize() - to retrieve the screen size and do my calculations.
However, I've just hit an unexpected problem. My activity is configured as landscape only, but if I start my application on emulator and onCreate() is called while the emulator is locked, the screen size returned by getSize() indicates portrait orientation. Once I unlock the screen, onCreate() is called again, this time correctly in line with expected landscape mode dimensions.
I'm not sure how to handle this situation. I see the following options:
for each onCreate() call perform full layout calculation and resource scaling again. This is the logically correct solution, but I don't want to the same work twice, just to throw away the first result.
if onCreate() is called for portrait mode, just do nothing, and set black background (I can see there's a silly rotate animation when I unlock the screen, so this would become pretty much a fade-in animation)
Actually I'd prefer second option, but I'm slightly afraid of any side-effects. Anyone faced this problem?
Update (2012-07-08):
I've probably assigned a slightly misleading title to this question (sorry!), as the problem is not in retrieving the dimensions itself, nor calculating the layout. It's more about the activity being first created in portrait mode, and then recreated in landscape mode again, despite being declared as landscape-only. I initially expected (reasonably, huh?) the activity to be created in landscape orientation only.
I eventually decided to fill the activity with black color when it's created in portrait mode, no side effects observed. On Android 4.0 I can see actual rotation animation when I unlock the screen - a bit strange, but well, I guess it is supposed to inform the user that she should rotate the phone. Given that in portrait mode I just fill the screen with black color, this animation looks sort of like a fade-in and rotation combined - perfectly acceptable.
Use that
DisplayMetrics dm=new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
Using this(Look code at down) only gives you screen size and if your views has static size they will be seen in different size on every different screen.
Display screen=getWindowManager().getDefaultDisplay().getSize();
How to use:
every screen has diffrent density. So use:
float density=dm.density;
with this density, you can set your views size like that:
(YOUR_ITEM_SIZE)*density;
also look here for additional information:
http://developer.android.com/reference/android/util/DisplayMetrics.html
if the emulator is locked , can't you assume that the user can't run anything anyway , so the app doesn't need to handle this end case ?
anyway , as bmavus wrote , use getMetrics for getting the screen size . also , if you need to change the screen orientation of the app , you can do so either in the manifest or in code.
for games , i would advice using opengl solutions , and if you don't have much time digging for it , you can use third party engines that can help you , such as andengine and libgdx.

Categories

Resources