Effectively measure the available screen - android

I need to find a way to actually get the dimensions of the screen of my device. I've been trying for long time now and there I cannot find a way to find the height and width in pixel of the drawable screen.
Here is the code I'm using right now:
SCREEN_HEIGHT = metrics.heightPixels - unusable_height;
Where unusable_height is found by:
//Calculate action Bar Height
if (getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)){
unusable_height = TypedValue.complexToDimensionPixelSize(tv.data,getResources().getDisplayMetrics());
}
//Calculate the navigation bar height
int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
unusable_height = unusable_height + getResources().getDimensionPixelSize(resourceId);
}
This works pefectly in my tablet however in my phone the resulting screen height is too large as stuff gets drawn off the screen. (And I calculate all my sizes according to the screen height). This is in landscape mode.
Any help? How can I know the total number of pixels from the end of the action bar to where I can't draw anymore.?
In my phone the navigation bar doesn't rotate so it stays where the bottom would be in portrait mode. So the bottom is the end of the screen. However in my tablet the navigation bar rotates and is put at the bottom of the screen.

I found a way to bypass the problem. However it is not neat. I'm posting it here in case anyone else needs it. It is possible to get the width and height by getting those variables form a layout or view whose LayoutParams were set both to MATCHPARENT. Of course this view needs to be that is set to setContentsView, so it should be like the master layout. However this will thrown non-zero values only when the view is already drawn, which means that you can't get its values in methods like on onCreate or onResume.
On the plus side, it should allways work and it is very simple.

Related

how to get fullscreeen height on S9 and similar?

My current android code to retrieve the screen height in pixels is this:
Resources.getSystem().getDisplayMetrics().heightPixels
However when the app is running in full screen mode on Samsung S9 (and probably on similar devices) the returned height is the same as it is when the app is not running fullscreen, i.e. it is calculated without the buttons menu bar that is gone when we're in fullscreen.
Any ideas on a better way to retrieve the height or possibly to be aware of the fact that the app is running full screen?
You can use
val size = Point()
windowManager.defaultDisplay.getRealSize(size)
size.y // will be your needed height
or in Java
Point size = new Point()
getWindowManager().getDefaultDisplay().getRealSize(size)
size.y // this one will be your height
Hope it's what you need.

Does display.getSize() include the black bar at the bottom of some devices?

In my Android app, I need to get the height and width of the view area in the app. I'm using the following code to get that information:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
However, I noticed that when I run the app in the emulator, the height variable also includes the black bar with the back, home and menu buttons. When I run it on my phone, I get a true value because my buttons are on the physical phone, not the screen. I don't have an Android device with the screen buttons to test with.
I'm curious, will my code include the height of the black bar? I would assume that the Android OS would not include the section taken up by that black bar as a usable part of the screen, but I'm not sure.
Is there some alternate way that I could get the height and width of the main relative layout when it's set as match_parent in the XML? This would probably be the better method, but I tried giving the layout an ID and targeting it in the Java code to get the size, but it crashed the app.
Any information or help would be very much appreciated.

Android: getting width available within layout

I'm wondering whether there is a way to find out the space available for Views within a layout.
To be a little more precise: in the activity two TextViews are displayed side by side. As the second one contains pretty much text, I'd like to check, whether this text can be displayed on the screen or whether it's too large. If that's the case I'll display a smaller version of the text instead (as Android doesn't display this text on multiple lines by default).
Currently I'm using some code like this:
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int screenWidth = screenSize.x;
if (tvTitle.getWidth() + tvDescription.getWidth() > screenWidth) {
tvDescrition.setText(getString(R.string.descritption_short));
}
But this doesn't work: the widths normally are smaller than the screen, so the long version is used. But: the text isn't displayed completely, the last words are always missing.
I guess the problem is, that the layout is using a padding and is therefore reducing the space really available.
So how to fix this? Any ideas are extremely welcome :-)
You can measure the width of a text(String) in pixels. Using this method:
Paint.measureText(titleString + descriptionString);
If that exceeds the total screen width - horizontal margin - horizontal padding then you can go for the short description.

How to make my android component as size dependent for various display screen sizes?

I am making an android component which allows user to pick date from it. It can be helpful for developer who wants user to select date in his app. In my basic view, I have TextView where date from pop up will be populated into it and I have a button beside TextView. When a User clicks on the button, my component gets popped out and displays Dates. The component gets pops out in a Popup window and shows dates as month view and user also can switch from next-previous months, next-previous years just like we do in Calendar. Check the Image.
http://s15.postimage.org/ujw8py60b/stackoverflow.jpg (Sorry, I couldn't upload an image here because I am not allowed as I am new User here)
Each date is a TextView with the width of 35 and height as 30 set by me. DaysDisplayBar is also of some size set by me. So this component's whole width is 245 and height is around 200. Which is for mobile screen size.
I want to make this component as size dependent for various screen display sizes. For e.g. If it is being viewed on Tablet or Pad, it should be bigger in size than what its size on mobile phone screen. That is, For various displays its size should be changed to some value like max 1/3 of display size or like that something.
What can be the solution for this? According to me, some mathematics is needed here, some formula, equations etc. how about Parabola? Please help, I am dumb in maths totally. Thanks! :D
"Each date is a TextView with the width of 35 and height as 30 set by me. DaysDisplayBar is also of some size set by me. So this component's whole width is 245 and height is around 200. Which is for mobile screen size."
^^ is the problem. Sizes should be defined relative to the layout, not absolute. For example, the calendar has 7 columns (one for each day). Instead of making each one 35px, make each 1/7th of the screen.
SO:
I am assuming a DaysDisplayBar is a row containing 7 TextViews (one for each day). If that is true, why not call it a Week? Either way, The trick is in layout_wieght. Make all elements fill_parent, and all with the same weight of 1. This will evenly distrubate all elements in the parent. In your case, the parent is a DaysDisplayBar.
SO:
set DaysDisplayBar attribute `layout_width="fill_parent"
For each TextViewset attribute layout_width="fill_parent" ANDlayout_weight="1"`
hope that helps!
First of all, make sure you use density pixels (dip) instead of pixels.
Second, you can get the screen width and height, and from there, calculate your component size.
You can get the screen dimensions using the Display class getSize() method:
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point screenSize = new Point();
display.getSize(screenSize);
int screenWith = screenSize.x;
Or you can get the parent view dimensions:
MarginLayoutParams params = (MarginLayoutParams)parentView.getLayoutParams();
int padding = parentView.getPaddingLeft() + parentView.getPaddingRight();
int margin = params.leftMargin + params.rightMargin;
int measuredWidth = parentView.getMeasuredWidth() - padding - margin;
That way you know how much space you have inside the parent view element for your component.
Remember to convert any hard coded value to dip, you can do it this way:
public static int getDensitySize(float size) {
float density = getResources().getDisplayMetrics().density;
return (int)(density * size);
}
You do all of this from your onMeasure method to set your view size, and later on the onDraw you'll use this measure to draw your component.

Screen Size Honeycomb Menu android

Hi I am trying yo get the screen size of screen minus the menu bar at the bottom.
I know I could just subtract a constant number according to the resolution of the device but it is a super ugly hack. I am sure google people are not dumb enough to forget to give the user a function to get the height of the bottom menu bar so I can subtract it from the full screen size
This is a similar post.
Size of android notification bar and title bar?
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
what_i_need =metrics.heightPixels-buttomMenuHeight();
I need buttomMenuHeight();
i could not find it in the API. I don't really care about the backward comparability at this point
Thanks
Why not use the height of the activity's top-level View? e.g. if your activity contains a full-height LinearLayout, use its height, so you don't have to know the menu height, or noticiation bar height.
I think you're treading a dangerous road doing this kind of calculation, as you don't know what the future layouts for Android might be, or how they might differ on Google TV, or whatever.
Try calling getHeight() after onLayout. The view is not sized on inflate.
EDIT:
Or since Activity does not have onLayout you might wait for onSizeChanged.
(Reference How to know when activity is laid out?)
I am drawing image in native code and I need to pass the size of the screen to C.
I tried to get the Height by getHeight() of the view but that returns 0 always even after setContentView is called.
RelativeLayout masterLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
masterLayout.setLayoutParams(params);
setContentView(masterLayout);
when I try masterLayout.getHeight(), it returns 0.
I expected to get the full screen height as I said fill_parent.

Categories

Resources