Is there a direct programmatic way to get a fill_parent View's pixel height and width? For instance a view in a grid layout in a tab. Or do I have to get the window size and subtract the static sizes of the views around it?
You can get the views parent by calling myView.getParent() and with getWidth() and getHeight() you get the pixel you need.
the documentation is providing more information to this topic
http://developer.android.com/reference/android/view/View.html
There's the onMeasure() method, not sure if that will do what you need. http://developer.android.com/guide/topics/ui/custom-components.html
Related
I want to arrange a set of custom widgets inside a GridView. There are predefined templates which describe a count of elements and arrangement config. Max width and heigh for each of them have to be provided in order to avoid scrolling. How can I get a size of the displayed GridView when computing items size in adapter's "GetView" method?
You have access to few functions in order to get a GridView Height and Width as any other View. Look at the documentation here.
The size of a view is expressed with a width and a height. A view
actually possess two pairs of width and height values.
The first pair is known as measured width and measured height. These
dimensions define how big a view wants to be within its parent (see
Layout for more details.) The measured dimensions can be obtained by
calling getMeasuredWidth() and getMeasuredHeight().
The second pair is simply known as width and height, or sometimes
drawing width and drawing height. These dimensions define the actual
size of the view on screen, at drawing time and after layout. These
values may, but do not have to, be different from the measured width
and height. The width and height can be obtained by calling getWidth()
and getHeight().
Try doing a quick search on their doc next time, even if it's Xamarin, the Android doc is pretty reliable and you only need some adjustment to make it work.
(e.g.) : grivView.Width; instead of gridView.getWidth(); one being a property on Xamarin to follow C# standards, the other is a function.
I have a custom View I'm making which has a fixed proportion between width and height. I want the programmer to set the height and I would like the View to set its own width according to a formula based on height.
The View is going to be a child of a RelativeLayout so I can't use the height of the parent element to calculate anything.
How can I accomplish this? I don't know how to do it in the onMeasure hook because the View's width and height are not available at that time.
By the way, I am making this view solely programmatically, no XML involved.
use View.setlayoutParams()
There's an example using an adView here.
I'm having a hard time finding example usage, or explanations, of the Android View concept of "measured state".
To be clear, this is different from measured width and measured height. See in Android docs.
The general idea is that a View has a desired width and height, which is constrained by two factors.
The first constraint is the parent View to which the View is attached. This will describe the maximum width and size of the View. If the View has asked to be wider or taller than the parent view, then it's measured height and measured width will be constrained such that the values are no greater than the parent's. These values, which can be MATCH_PARENT, WRAP_CONTENT or a dp value, get converted to measured widths and heights once the parent View's width and height constraints are included. This guarantees that no child is larger than it's parent.
The second constraint comes from the View's siblings, or in other words, from other Views attached to the same layout. The ViewGroup (Layout) will resolve a View's width and height such that it displays correctly. These resolved width's and height's are the values you get from getWidth() and getHeight().
There is a lot of good documentation on the Android development portal. In particular look at the Layout section of the View class: http://developer.android.com/reference/android/view/View.html#Layout
Edit: Sorry for misunderstanding your question. I've taken a look at the concept of measured state for a view, and I can only find a single documented state: MEASURED_STATE_TOO_SMALL. This leads me to believe that it's use may be very limited and it's existence is primarily for the purpose of future functionality or to be made use of by custom Views/ViewGroups.
The documentation for MEASURED_STATE_TOO_SMALL states the following:
Bit of getMeasuredWidthAndState() and getMeasuredWidthAndState() that
indicates the measured size is smaller that the space the view would
like to have.
This leads me to believe that the bit will be set whenever a fixed dp/px value is given to the View which is larger than the parent's width and height and therefore the View's measured width and height will have been scaled down.
If you manage to find more states or additional information as to where it is used, please to update the question. All the best!
I wrote my own GridView and i using it with XML setting layout_width as match_parent. My question is how to get width in pixels from attrs. I know how to get my cyston attrs but dont know how to read width value before view is drawed.
In android you can't access the attribute of size before drawing because the dimensions are determined when the drawing process begin. It is the same for all views describe by xml.
I found this link that try to find a way to get the height so you could use the same to find the width of the screen (if your grid fill the size of the screen) : Android - get Height of the parent layout
They give :
getApplicationContext().getResources().getDisplayMetrics().heightPixels
Hopes it helps
If you find it useful please check Resolved
I solve this. Just need to getMeasuredDimensions after onMeasure method is called.
I'm creating a custom component, where can I know the dimensions this can assume on it's parent? In Layout it's defined with fill_parent x fill_parent.
I tried getWidth() x getHeight() on the constructor, on the onMeasure method and onFinishInflate, in all cases this return 0, I wish to know the size it's have to draw some components in independent screen size.
You can override onMeasure() and get values from there.
http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)
In onMeasure you can actually set your own dimensions for the component and build component accordingly to sizes after that.
Use View.MeasureSpec.getSize(one of the parameters methods takes in. Either width, or height) to get dimensions.
getWidth() and getHeight() will only be populated after the Layout Phase, that is after onLayout() has been called.
You should not use the values within onMeasure() or getMeasuredWidth()/getMeasuredHeight(), since those are only the dimensions the View is initially offered or would like to have, not necessarily the ones it actually ends up getting.
See http://developer.android.com/guide/topics/ui/how-android-draws.html for more information.