Dynamic height of ImageView in scrollView - android

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

Related

Android: Layout height in percentage of screen resolution inside scroll view

I have a scrollView with a contstraintLayout inside , i need to set the same height value in percent of screen independently from the scrollView and his content height
for example:
if it's possible not programmatically but via XML properties.
Already tried layout_constraintHeight_percent, but it follow the parent height and is not what i need

First child of LinearLayout inside ScrollView with 1/3 space of device height

I have a ScrollView with a LinearLayout with 3 elements inside. I would like that the first element has a height of 1/3 the height of the device height and the other 2 with wrap_content, is this possible to do in xml or how would you do this? Using weight alone it does not seem possible because the LinearLayout inside the ScrollView could be longer than the device's height.
According to Android docs, android:layout_weight:
Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
This implies that the weight is not dependent on the screen height, but rather only on its parent view, so you can't achieve that from xml. To achieve this I would compute the height of the screen and then resize the view:
Display screen = getWindowManager().getDefaultDisplay();
Point size = new Point();
screen.getSize(size);
int screenHeight = size.y;
myView.setLayoutParams(new LayoutParams(myView.getWidth(), screenHeight / 3));
You can give the first linear layout weight=0.333 with height=0 and the other two layout wrap_content

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.

HorrizontalScrollView Full Width

Well, I've developed a HorizontalScrollView with some buttons inside.
I'm able to get the width of this HorizontalScrollView through getWidth(). But that returns the width of the displayed HorizontalScrollView, not the full extension.
How could I get the full width? I mean, the full width including the buttons that are not displayed on the screen?
Cheers.
Your HorizontalScrollView should have only 1 child (a horizontal LinearLayout or something). Try getting the width of that view.

How to make buttons, with dynamic text, the same size and width to height ratio?

I have a few buttons I want to display in a row in a decision tree. I'm trying to get the buttons in each row of the tree to be the same size. But the buttons have different text. Some have a couple words and some have a sentence. Is there a way I can get the buttons to all be the same size and all maintain the same width to height (4:3) ratio?
I can get the heights the same using fill parent on the button heights while having the parent row layout_height set to wrap_content. How can I get the width of the buttons to be the same size while maintaining a 4:3 width to height ratio. (The width to height ratio constraint is so I don't end up with really tall and thin buttons, which would look silly)
Anyone have any suggestions?
Edit:
I just saw this link which is kinda what I am looking for: Scaling layout with invariant aspect ratio in Android
My problem with this is that it will inflate all the buttons and introduce a lot of empty space around the text inside.
If I could find a way to incrementally increase the width or height (whichever is smaller) of the button and then resize it so it re-"wraps_content", then this could work. Anyone know how to re-wrap the content?
If you are able to get the same height for all you can use android:layout_weight="1" for all buttons and put android:layout_width="0dip" so all buttons will have same width

Categories

Resources