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.
Related
Using the ViewPager view from the Android support library, the default setup shows one page at a time, with a large margin between each item - ie, if your view is about half the width of your activity there's space on either side, and as you swipe the next one in there's space there too.
ViewPagers have a method, setPageMargin(), that lets you specify an offset to adjust the margin size between pages, and I'm using it to specify a negative margin so that it pulls the pages closer together. However, obviously the amount you need to pull in these margins varies according to the screen dimensions.
So, I'm looking for a smarter way: is there a way to tell the ViewPager "I want no margins at all, making the views in my ViewPager butt up against each other"?
Thank you!
I don't know if there's a smarter way, but your way should work if all pages have the same width. Try something like this:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// getActivity().getWindow... if inside a Fragment
yourViewPager.setPageMargin((yourPageWidth - dm.widthPixels) / 2);
I've seen a ton of different questions and answers to the problems people are having with retrieving height and width of views, particularly this thread.
The following are mentioned methods to retrieve View dimensions:
onWindowFocusChanged() in your activity
Subclassing the type of view you're using and overridding either onSizeChanged(), onLayout(), or onMeasure()
Using a ViewTreeObserver and addOnGlobalLayoutListener()
Each of them seem to work in some cases, while in other cases people say something doesn't work correctly. Since there isn't a setOnDimensionsKnown(OnDimensionsKnown odk) or similar method for the View class, which of these methods (or possibly one not mentioned) will give me the dimensions my view will eventually have, regardless if it's drawn yet, has wrap_content or fill_parent parameters, or it has explicit height and width set in px, dp, or similar?
EDIT: Perhaps a specific example would be helpful, I'm trying to make a PopupWindow wrap it's contents, and to be offset in the -x direction by the value of it's width. The problem is that the contents width, and thus the popupwindows width, are not measured until after I show the popup. So basically I can't think of a good way to measure the width it will be before it is drawn to screen.
My usual approach is to override whatever view class I need the dimensions of before it being drawn, and create my own setOnDimensionsKnown(OnDimensionsKnown odk) method and fire it with the width and height values that onMeasure is called with. This works it every case I've needed, but it doesn't seem very elegant to override every view class to do this.
I can post code to help explain the example more.
Good way of doing it is adding an OnGlobalLayoutListener() to the view, as stated in this answer (point 3 in your question, in my opinion that's the most reliable for any View). The way I do it for a PopupWindow is to inflate it's layout and then call measure on it:
popupView = getActivity().getLayoutInflater().inflate(R.layout.popup_layout, null);
popup = new PopupWindow(popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
popupView.measure(view.getMeasuredWidth(), view.getMeasuredHeight()); //the view is parent's layout (not sure if those are correct values, but it measures okay in my case)
and then just get the numbers by calling popupView.getMeasuredHeight() and .getMeasuredWitdh().
Your problem is probably long gone by now, but hopefully someone else will stumble upon this and find it helpful. :)
I have a RelativeLayout as root element. There are three LinearLayouts under root element. First and last has fixed height and the middle one takes rest of screen.
There are two ScrollViews inside the middle LinearLayout. I programatically add new views in them. I want to show three item in a scrollview no matter the screen size is.
The problem is, I can't calculate the height of are so I can't divide it by three and get item height required.
I tried to call measure() and getMeasuredHeight() but LinearLayout returned 21 (which I have no idea why) and ScrollViews returned 0. Both LinearLayout and ScrollViews has match_parent attribute.
So how can I get the actual height? I know it is calculated somewhere because I can see it covers the all empty area.
The Good
Don't unless you have a really good reason. This kind of layout won't work for tablets and larger devices (it will look very odd having giant list items) and it is not common behaviour.
The Bad
You said the top and bottom views have a fixed size, and you can get the height of the screen with:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
// Use getSize on API13+
int displayHeight = display.getHeight();
Then the height of each row is just
int listHeight = displayHeight - (2 * fixedHeight)
int rowHeight = listHeight/3;
This is bad because it will not work in more complex layouts.
The Ugly
In onCreate the size of your views will not yet have been initialised. You need wait until the view has been given a size. There a few ways you could do this:
Use a View.OnLayoutChangeListener (API 11+)
Use a ViewTreeObserver.OnGlobalLayoutListener (API 1+)
Create a custom View class (subclass LinearLayout) and override onSizeChanged
I am using a relative layout with 4 buttons (in the center, one below the other).
My problem is to adjust the gap between all buttons so it will be the same between all buttons.
I want to do it dynamically according to the height of the screen (i use Display class to get the actual height).
what is the best way to do it?
Thanks,
Amigal
You can do this via modifying the LayoutParams of your View
Button b;// your button
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)b.getLayoutParams();
lp.leftMargin=10;//your left margin here
lp.topMargin=10;//your top margin here and do this for other 2 margins if you need to
sometimes you need to call
b.setLayoutParams(lp);
to have the changes applied
also i dont know how you get the screen dimensions, but this works at every API:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
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.