When get the real width? - android

I have set programmaticaly the width of a button to 0dp and a weight of 1 so that it has the same width of the rest of the buttons of the gridview.
The problem is that i cannot succeed in setting the height equals to the width as the width has been set to 0.
So i would like to know when the button takes its real width. For now, i'm using viewTreeObserver but i only get the good width when I click on the button.
Do you have any solution for this ?

Make a custom view subclass and override onMeasure. In your onMeasure, look at the measurespec to find out how big you can be, and set your appropriate width and height.
onMeasure is the function your parent view calls to figure out the size of its subviews. Make sure to call setMeasuredDimension inside onMeasure, or you'll throw an exception somewhere.

Related

How to control what happens when a view's wrap_content exceeds its parent's width/height?

In Android, given any View - I want its height/width to both be wrap_content - but I don't want this value to ever exceed its parent. How do I achieve this in XML?
The width/height of a View can never exceed its parent. Just use wrap_content.

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width. How can I do it? Thanks.
You can accomplish that in multiple ways. It only depends if you really need to stick with the FrameLayout.
Two easiest would be those:
FrameLayout - you would need to know whats the width of the layout (so if your layout is wrap_content it might get tricky). Set your view's gravity to left, and set its margin_left to half of the layout's width.
LinearLayout - either put it inside your FrameLayout (not very good practice), or (if you can) swap it with your FrameLayout. Set it's orientation to horizontal, add dummy view with width = 0dp, weight = 1. Then add your view with the same values for width and weight.

Get size of GridView when creating items within its adapter

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.

Basing a custom View's width off of its height

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.

getting a view WIDTH with match_parent property?

I have an imageView with layout_width="match_parent".
On my adapter's getView(), I'd like to get the actual width of this imageView but don't know how.
I tried using imageView.getWidth(), but this returns 0.
The view is not attached to its parent group in getView(), so it hasn't been measured, hence the zero width. The only way I can think of is to do a parent.getWidth() which returns the width of the ViewGroup. This will only work when your view is not going to be placed next to another view in parent. This is usually the case for ListView, so may work for your case.

Categories

Resources