Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10083

Layout height getting as -2

The below is the xml that iam inflating within my customList

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <exp.viswanath.dropper.CustomView
        android:id="@+id/custom_view"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="#e1e1e1"
        android:padding="4dp" />
    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/custom_view" />
</RelativeLayout>
</LinearLayout>

And my custom view Layout is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<exp.viswanath.dropper.CustomList
    android:id="@+id/wrapper"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>

Iam getting the height of Layout with id parent and listview as -1 and -2 . But iam getting the correct height for element with id custom_view as 105. Iam inflating this within the constructor of my CustomList

CODE

    View rowView = LayoutInflater.from(context).inflate(R.layout.activity_dropper, null);

    parentLayout = (RelativeLayout)rowView.findViewById(R.id.parent);

    Log.d("", "DEBUG:pl:" + parentLayout.getLayoutParams().height);

    myView = (CustomView)parentLayout.findViewById(R.id.custom_view);
    _viewHeight = myView.getLayoutParams().height;

    initList();

    _height = (-1) * _viewHeight;
    Log.d("","height:" + _height);
    listView = (ListView)parentLayout.findViewById(R.id.listView);

    Log.d("", "DEBUG:lv:" + listView.getLayoutParams().height);

Does any one knows the problem or experienced the same? The layout is not visible at all

PROBLEM

MY VIEW IS NOT VISIBLE

Upvotes: 0

Views: 156

Answers (2)

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Try this way

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

            @Override
            public void onGlobalLayout() {
                Log.d("", "DEBUG:lv:" + listView.getLayoutParams().height);
            }
        });

Upvotes: 0

Amulya Khare
Amulya Khare

Reputation: 7708

The problem is that myView.getLayoutParams().height; will not return you the height as it has not been measured yet. In tghis case it returns you the constant associated with the view such as FILL_PARENT, WRAP_CONTENT etc. See documentation here:

Information about how tall the view wants to be. Can be one of the constants FILL_PARENT (replaced by MATCH_PARENT , in API Level 8) or WRAP_CONTENT. or an exact size.

To get the size of a layout, your should be using getWidth() or getMeasuredWidth() methods. However, these methods won't give you a meaningful answer until the view has been measured. Read about how android draws views here.

You can get the correct size, by overriding the onWindowFocusChanged() as mentioned in this thread.

Alternatively, you could do this:

    myView.post(new Runnable() {
        @Override
        public void run() {
            Log.d("", "DEBUG:lv:" + myView.getMeasuredHeight());
        }
    });

Upvotes: 2

Related Questions