Basing a custom View's width off of its height - android

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.

Related

When get the real width?

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.

custom view with height based on own width

I am currently developing an Android App for Tablets, landscape mode only.
My question:
How can I display a View with a height based on the width?
A LinearLayout holds my View so that I can use the weight attribute.
My height always has to be 1/3 of my width while the width always has to be a specific percentage of the parent, in this case 80%(sum weight is set to 1, weight is 0.8) How can I achieve this?
Thanks in advance.
You have to override the onMeasure() method of view and customize this functionality. Alternatively you can use the library
https://developer.android.com/reference/android/support/percent/PercentRelativeLayout

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.

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.

Android: Is it possible to set View height proportional to the View width?

I'd like to set the View width into the screen width and the View height into 0.8 * View width. Is it possible to achieve this from the XML Layout?
No, but it could easily be done by creating a custom subclass of the view you want and overriding onMeasure to return those sizes.

Categories

Resources