set Layout Params at runtime - android

I have an array of Pages, each Page contains content and a specific layout.
I have a Horizontal Pager that can switch between pages when scrolling through them.
My question is, how do I set the width and height of the LinearLayout at runtime so the Page is set properly??
Atm the LinearLayout is set to 1024px width 768px height for landscape mode. This should be changed.

I found out how to do it. Should've read the docs better.
fl.setLayoutParams(new FrameLayout.LayoutParams(width, height));

Related

Resize ImageView programmatically

I'm working with an ImageView and I want to resize it programmatically passing (for example) from a full-screen ImageView to a 50x50. Is there a way to do that?
Different from the one you suggested me to see because I don't need to fit the image in the ImageView but to resize the dimension of the ImageView.
My suggestion use a relative layout and put image inside that has with weight and able to auto resize which depends on the weight. The best way to auto resize even your screen rotates.
I didn't remember which one layout has weight format
You can use LayoutParams to set height width programmatically -
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width = 30;
layoutParams.height = 30;
imageView.setLayoutParams(layoutParams);
ConstraintLayout provides a mechanism for fixing the aspect ratio of a child view. Select the child view that we want to control, and then set the ratio value, we can manually put the ratio that we want to set.
Using constraintLayout makes UI shrink or expand according to screen size without distorting the UI. This is the most important advantage of using ConstraintLayout in UI.

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

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width. How can I do it? Thanks.
You can accomplish that in multiple ways. It only depends if you really need to stick with the FrameLayout.
Two easiest would be those:
FrameLayout - you would need to know whats the width of the layout (so if your layout is wrap_content it might get tricky). Set your view's gravity to left, and set its margin_left to half of the layout's width.
LinearLayout - either put it inside your FrameLayout (not very good practice), or (if you can) swap it with your FrameLayout. Set it's orientation to horizontal, add dummy view with width = 0dp, weight = 1. Then add your view with the same values for width and weight.

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.

How to change size of Android ScrollView

I have a layout like this (abstracted):
Scrollview - fill_parent
LinearLayout - wrap_content
ImageView 1 - wrap_content
ImageView 2 - ...
ImageView 3
...
In the course of user interaction some images are replaced by larger or smaller ones, shortening or lenghtening the area to be scrolled.
And that is the problem. The Scrollview (SV) does not know about the change, so either it scrolls over a lot of empty space at the bottom, or cuts off a picture or two at the bottom.
Question 1: Can I somehow make the SV readapt to the changed hight of the LiearLayout (LL)?
Question 2: I can obtain the current size through getHight on the LL. But supplyong it to the SV via changed LayoutParams does not work - of course, that would only change the height of the SV on the screen. Is there a way to put the changed height of the LL into the SV in the code, somehow?
Question 3: I havn't tried it yet. Would creating the SV, LL and its children in code and then adding/removing children of changed size as required, make the SV adapt to the changes?
And last question: Is there a better aĆ¼pproach, except using ListViews?
Have you tried calling requestLayout() when images change?

Categories

Resources