I finally realized it myself if widgets are not displayed on the screen, they are not created. thus, button elements that are still at zero because they never turned out; and they never showed up because the scroll view pushed them away from the screen. so I attacked the problem from a different angle, this time, what I did was set the scroll view to an arbitrary small number so that the buttons stay on the screen to get the size of the top-level layout, which is stored in "int totalsize", then I get the sizes of all the elements except the scroll, and the resulting fields for each view, and also summed them all up to "int used", then I set the scroll view height to "totalalsize-used", and this is what worked. I found that I manually typed in the fields that do not work, when the screen size changes, the fields also change, so it’s best to open them along with the viewing sizes.
in my create method, I got this:
final Button tv = (Button)findViewById(R.id.saveit); ViewTreeObserver vto = tv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { ViewGroup.MarginLayoutParams vlp = (MarginLayoutParams) tv.getLayoutParams(); int btnsize =tv.getMeasuredHeight()+vlp.topMargin; sizeit(btnsize); ViewTreeObserver obs = tv.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } });
and then the size of its function
private void sizeit(Integer thebuttons) { View v = findViewById(R.id.viewmain); int total = v.getHeight(); v = findViewById(R.id.view1); ViewGroup.MarginLayoutParams vlp = (MarginLayoutParams) v.getLayoutParams(); int used=v.getHeight()+vlp.topMargin; v = findViewById(R.id.view2); vlp = (MarginLayoutParams) v.getLayoutParams(); used+=v.getHeight()+vlp.topMargin; v = findViewById(R.id.fonttext); vlp = (MarginLayoutParams) v.getLayoutParams(); used+=v.getHeight()+vlp.topMargin; v = findViewById(R.id.infolabel); vlp = (MarginLayoutParams) v.getLayoutParams(); used+=v.getHeight()+vlp.topMargin; Integer scrsize=total-used-thebuttons; v = findViewById(R.id.scrview); ScrollView scr = (ScrollView)findViewById(R.id.scrview); ViewGroup.LayoutParams scrlprams = scr.getLayoutParams(); scrlprams.height=scrsize; scr.setLayoutParams(scrlprams); }
hope this helps someone also deal with the same problem
source share