custom view with height based on own width - android

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

Related

Dynamic height of ImageView in scrollView

I am trying to add 3-4 imageviews in a scroll view. Imageviews have dynamic heights. Height of first view have 70% of the screen and second and third have 30% height. I am using constraint layout and guidelines but the height is getting set according to the height of the scroll view but not according to the screen height. Is there a way to do this with constraint layout.
Have you tried doing it programmatically?
First, get the Screenheight for your device: How to determine the screen width in terms of dp or dip at runtime in Android?
Secondly, get your Imagview and set its height according to xx% of the determined screen height.
BR

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.

Android - How can I determine an appropriate height for my ListView items?

I am building an Android app with a ListView. Coming from iOS I am used to setting fixed pixel heights for list view items, since the screen sizes of the used devices are always the same. Now for Android, I am wondering what is a good way to dynamically set the heights of ListView items so that it it looks nice on all screen sizes?
In android there are two famous properties. They are:
MATCH_PARENT formerly FILL_PARENT using this property for layout width or height will expand the view to the parents width or height minus margins
WRAP_CONTENT using this property for layout width or height will allow the view to take as much space required or available(if it exceeds screen dimension exception is inside scrollable views)
So for your tag set both width and height to match_parent. And in the custom row that you might be populating set the root layout width to match_parent and height to wrap_content.
Note: in android while we give fixed height at times but it is generally not a good practice.

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.

Is there any way to use screen height and width in xml builder in Android?

Is there any way to use screen height and width in xml builder in Android? I want to set View height to be half of screen without using view weight. Is it possible?
The best way to handle screen height and width is to set view's sizes programmatically in onCreate() method.

Categories

Resources