bubu uwu
bubu uwu

Reputation: 483

get width, height from Layout

I need to get width and height of Linear layout, however I do not know when the Relative Layout is ready to provide me these two attributes.

but it only returns 0.

my screen enter image description here

please check my code, i have no idea.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        LinearLayout container = (LinearLayout)findViewById(R.id.container);
        int width = container.getWidth();
        int height = container.getWidth();

        Log.d("MainActivity onCreate", "width #" + width + ", height #" + height);
    }
}

and activity_main.xml layout

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:background="#FFFFFFAA"
        android:orientation="vertical"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </LinearLayout>

</RelativeLayout>

how do i fix this?

Upvotes: 0

Views: 3726

Answers (5)

Piyush
Piyush

Reputation: 18933

Whenever you try to get dimensions on onResume() or on onCreate() method, it always returns 0. So you should use ViewTreeObserver

ViewTreeObserver vto = container.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
    public boolean onPreDraw() {
        container.getViewTreeObserver().removeOnPreDrawListener(this);
        finalHeight = container.getMeasuredHeight();
        finalWidth = container.getMeasuredWidth();
        return true;
    }
});

Also you can customize your View which will extends with LinearLayout and in onMeasure() method you can get width and height.

Upvotes: 3

makasujan
makasujan

Reputation: 83

First of all replace

 int height = container.getWidth();

by

 int height = container.getHeight();

container.getHeight() will return 0 (as well as container.getWidth()) if it's not drawed yet.

Solution would be to get the height after your view is layouted:

container.post(new Runnable(){
    public void run(){
     int height = container.getHeight();
    }
});

Upvotes: 2

yennsarah
yennsarah

Reputation: 5517

First, you are calling getWidth twice - for width and height.
You can try it with measureSpecs, like this:

int matchParentMeasureSpecWidth = View.MeasureSpec.makeMeasureSpec(((View) container.getParent()).getWidth(), View.MeasureSpec.EXACTLY);
int matchParentMeasureSpecHeight = View.MeasureSpec.makeMeasureSpec((View) container.getParent()).getHeight(), View.MeasureSpec.EXACTLY);
v.measure(matchParentMeasureSpecWidth, matchParentMeasureSpecHeight);

int height = container.getMeasuredHeight();

Upvotes: 1

Nik Patel
Nik Patel

Reputation: 627

Try this

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

    final LinearLayout container = (LinearLayout)findViewById(R.id.container);

            container.post(new Runnable()
            {

                @Override
                public void run()
                {
                    Log.i("TEST", "  Layout width : "+ container.getWidth() +"  Layout height "+ container.getHeight()) ;

                }
            });

Upvotes: 1

Eden
Eden

Reputation: 4196

You can try and mesaure it first. But make sure you do so only after every other view as been added beacuse your LinearLayout is depended on other views.

LinearLayout.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
LinearLayout.getMeasuredWidth();

Upvotes: 1

Related Questions