0

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.

  • Possible duplicate of [Android set height and width of Custom view programmatically](http://stackoverflow.com/questions/5042197/android-set-height-and-width-of-custom-view-programmatically) – e-sushi Nov 19 '16 at 12:42

1 Answers1

0

use View.setlayoutParams()

There's an example using an adView here.

Community
  • 1
  • 1
aProperFox
  • 2,114
  • 2
  • 20
  • 21
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/14340636) – Insane Skull Nov 19 '16 at 13:09
  • @InsaneSkull, fair point. But when the question doesn't have any relevant code, it seems a bit redundant ;) – aProperFox Nov 21 '16 at 22:23