Android - programmatically specify the height and width of the new ScrollView

I am new to Android and I am working on a tutorial on programmatically creating a layout instead of doing it via xml, I am just stuck, can someone advise, please.

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

Is this possible, and so I can just specify the dimensions of the ScrollView and get subsequent views to inherit it.

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.

+4
source share
3 answers

Try to configure it as follows:

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

The linearlayout function should now fill the scroll.

+18
source

You tried to do something like

 home_scroll.setLayoutParams(..,..); 

you can find here here

 setLayoutParams(ViewGroup.LayoutParams params) Set the layout parameters associated with this view. 
+1
source

A simple way to give a dynamic 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); 
+1
source

Source: https://habr.com/ru/post/1337145/


All Articles