Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

okay here's my issue I want to create a scroll view that fills down to a set of two buttons that are right at the bottom of the screen. knowing i can't just set the scroll view to fill parent I thought I'd go and get the height of the linear layout with the buttons in it and then the height of the scroll view that I set to fill 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 to that works to measure all of the other views in the activity but with this one it always returns a zero and I'm at a loss as to how to get the height of it.

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 the height of it as I expect but set it to this one and I get 0; and this is the one I'm trying to measure! I've 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>
like image 383
Kit Ramos Avatar asked Dec 13 '22 06:12

Kit Ramos


1 Answers

I finally Figured it out myself if the widgets do not appear on screen they do not get created. so thus the button elements where still at zero because they where never rendered; and they where never rendered because the scroll view pushed them off 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 the buttons stay on screen, get the size of the top level layout stored that to "int totalsize", then I get the sizes of all the elements except the scroll view and got the margins for each view as well and totaled them all up into "int used", then I set the scroll view height to "totalsize-used" and that is what worked. found it I totaled up the margins manually that doesn't work when the screen size changes the margins also change so discovering them along with the view sizes works best.

in my on 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 it 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 fighting with the same issue

like image 93
Kit Ramos Avatar answered Jan 31 '23 01:01

Kit Ramos