Search code examples
androidandroid-layoutandroid-linearlayoutandroid-constraintlayout

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.


Solution

  • 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