How to programmatically get the height of a view with buttons on it?

oky here my issue I want to create a scroll view that populates up to two buttons located at the bottom of the screen. knowing that I can’t just set the scroll view to populate the parent, I thought I would go and get the height of the linear layout using the buttons in it, and then the scroll height that I set to fill the parent. then subtract one from the other, and then reset the scroll view height. I figured out how to get and set the scroll view height. and I found a way that works to measure all other views in activity, but with that it always returns zero, and I don’t understand how to get its height.

final LinearLayout tv = (LinearLayout)findViewById(R.id.thebuttons); ViewTreeObserver vto = tv.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { public void onGlobalLayout() { Integer grr = tv.getHeight(); Log.e("the h", grr.toString()); ViewTreeObserver obs = tv.getViewTreeObserver(); obs.removeGlobalOnLayoutListener(this); } }); 

now, if I set it to any of my other layouts, I get its height as I expect, but set it to this, and I get 0; and this is the one I'm trying to measure! I also tried to measure the button directly to no avail

 <LinearLayout android:id="@+id/thebuttons" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="5sp" > <Button android:id="@+id/saveit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" /> <Button android:id="@+id/clearit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" /> </LinearLayout> 
+4
source share
1 answer

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

+12
source

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


All Articles