Android - Programmatically specify height and width of new ScrollView
Asked Answered
P

3

5

I'm new to Android and I'm working through a tutorial on programmatically creating a layout instead of doing it through the xml, I'm sorta stuck can someone advise please.

So I have a ScrollView then have added a LinearLayout into it, I want both to be a specific size - 480 x 800 (code below). I was able to set the size in the LinearLayout but I cannot get the ScrollView to also be that size but I can't find how to do it.

Is this possible, and can I therefore just specify the ScrollView dimensions and get the subsequent views to inherit that.

ScrollView home_scroll = new ScrollView(this);
LinearLayout home_linear = new LinearLayout(this);
home_linear.setOrientation(LinearLayout.VERTICAL);

home_scroll.addView(home_linear, new LinearLayout.LayoutParams(480, 800));

Any help would be greatly appreciated, thanks.

Pomeroy answered 26/1, 2011 at 13:49 Comment(0)
C
19

Try setting it up like this:

home_scroll.setLayoutParams(new ViewGroup.LayoutParams(480, 800));
home_scroll.addView(home_linear, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

The linearlayout should now fill the scrollview.

Caracal answered 26/1, 2011 at 14:25 Comment(1)
Any idea how to do that under a Constraint Layout? Thanks for help!Glaring
N
1

Did you try doing something like

home_scroll.setLayoutParams(..,..);

you can find these here

setLayoutParams(ViewGroup.LayoutParams params) 
Set the layout parameters associated with this view.
Noctambulous answered 26/1, 2011 at 14:12 Comment(1)
Thanks for reply, I was trying that but getting:The method LayoutParams(int, int) is undefined for the type ScrollView. Apologies for being a complete novice, I'm trying to learn as I go.Pomeroy
P
1

Simple way you can give dinamic width and height:-

 LayoutParams layout = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            layout.setMargins(screenWidth / 5, screenHeight - cardHeight - 30, 0, 0);
            tvConftxt.setLayoutParams(layout);
Pigment answered 5/12, 2013 at 12:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.