Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get wrap_content or match_parent layout width and height in android?

I want to get my layout or view (not screen size) width an height in some android devices, so how can i do this? For example:

<LinearLayout
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:paddingRight="8dp"
     android:paddingLeft="8dp"
     android:id="@+id/layout_scrol_left"/>

I have this layout and i want to get layout width and height size (in dip) in runtime, how can i do that?

like image 435
MrNadimi Avatar asked Mar 11 '23 15:03

MrNadimi


1 Answers

First of all you need to take your layout.

LinearLayout ll_left = (LinearLayout)findViewById(R.id.layout_scrol_left);

Now if ll_left is not yet drawn both ll_left.getHeight() and ll_left.getWidth() will return 0.

So you have to get them after your view is drawn:

ll_left.post(new Runnable(){
    public void run(){
       int height = ll_left.getHeight();
       int weight = ll_left.getWidth();
    }
});
like image 175
Daniele D. Avatar answered Apr 20 '23 01:04

Daniele D.