Nadimibox
Nadimibox

Reputation: 1444

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?

Upvotes: 4

Views: 8560

Answers (2)

SepJaPro2.4
SepJaPro2.4

Reputation: 635

i think you can use something like

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

and you can use the methodes below after the UI thread has been sized and laid

    myview.getHeight();
    myview.getWidth();

hope this helps .

if you problems using the method above take a look at getWidth() and getHeight() of View returns 0

Upvotes: 1

Daniele D.
Daniele D.

Reputation: 2734

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();
    }
});

Upvotes: 8

Related Questions