How to get Width of Imagebutton

Asked

Viewed 15 times

2

How can I get the width of an Imagebutton defined in XML 0 being defined by weight?

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginLeft="10dp"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/fluido"
        android:layout_width="0dp"
        android:layout_height="70dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:tint="@color/icon"
        android:layout_weight="0.33"
        app:srcCompat="@drawable/fluido" />

        <......../>

</LinearLayout>

How do I get this value that was automatically set by Weight?

How is set in the XML as 0, if I do: imageButton.getWidth(); or imageButton.getLayoutParams().width(); both return 0 as is in XML.

1 answer

0


I got it here:

view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

    @Override 
    public void onGlobalLayout() { 
        view.getViewTreeObserver().removeGlobalOnLayoutListener(this);

        // Dimensões corretas:
        int width  = view.getMeasuredWidth();
        int height = view.getMeasuredHeight(); 
    } 
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.