Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getMeasuredHeight() and width returning 0 after measure()

I'm really confused with this whole measuring thing that android does for layouts. Basically, I want to get the actual computed height and width of a view when it has been laid out BEFORE it gets laid out. I need to get the computed height and width because I have a hidden LinearLayout that I want to animate when opening using viewpropertyanimator. I need to supply the target width (which is the computed width) to the animator before it animates it. The width of this layout is dynamic because it relies on its android:weight parameter for the width, so I can't give a static value.

Here's my activity class:

package com.example.testproject;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class TestActivity extends Activity
    {

    private LinearLayout gmailListContainer, yahooMailListContainer, windowsLiveListContainer;
    private LinearLayout emailMessageContainer;

    private Display display;



    @Override
    protected void onCreate(Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.activity_test);

        gmailListContainer = (LinearLayout) this.findViewById(R.id.email_activity_gmail_layout);
        yahooMailListContainer = (LinearLayout) this.findViewById(R.id.email_activity_yahoo_mail_layout);
        windowsLiveListContainer = (LinearLayout) this.findViewById(R.id.email_activity_windows_live_layout);
        emailMessageContainer = (LinearLayout) this.findViewById(R.id.email_activity_email_content_layout);

        gmailListContainer.setBackgroundColor(Color.CYAN);
        yahooMailListContainer.setBackgroundColor(Color.MAGENTA);
        windowsLiveListContainer.setBackgroundColor(Color.GREEN);
        emailMessageContainer.setBackgroundColor(Color.RED);

        display = getWindowManager().getDefaultDisplay();

        setMeasuredDimensions();
        }

    private void setMeasuredDimensions()
        {
        View v = this.findViewById(R.id.email_activity_layout);
        v.measure(display.getWidth(), display.getHeight());
        Log.v("EmailActivity", v.getMeasuredHeight() + ", " +v.getMeasuredWidth());
        }

    private void setWeight(LinearLayout container, float weight)
        {
        LinearLayout.LayoutParams params = (LayoutParams) container.getLayoutParams();
        params.weight = weight;
        container.setLayoutParams(params);
        container.setVisibility(View.VISIBLE);
        }

    }

Here's the relevant layout resource:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/email_activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">

<LinearLayout
    android:id="@+id/email_activity_gmail_layout"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />
<LinearLayout
    android:id="@+id/email_activity_yahoo_mail_layout"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />
<LinearLayout
    android:id="@+id/email_activity_windows_live_layout"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />
<LinearLayout
    android:id="@+id/email_activity_email_content_layout"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="0"
    />

like image 627
Neilers Avatar asked Aug 14 '12 05:08

Neilers


1 Answers

Add the below method to your Activity and call your method from it.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
      setMeasuredDimensions();
    super.onWindowFocusChanged(hasFocus);
}

In onCreate() you can't assure that your view is drawn. It might take some time. But this above method gets called only when the view is drawn completely. So YOu should be able to get the width and height exactly here.

like image 141
Andro Selva Avatar answered Nov 01 '22 18:11

Andro Selva