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

Answers


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

Try this way

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

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

Need Your Help

the output of cv::imwrite vs cv::imencode

c++ opencv jpeg

Does cv::imencode have an identical behavior to cv::imwrite when converting cv::Mat to JPEG? I know that first one write to buffer and the second write to file but I am asking about the written con...

Is there a pure virtual function in the C++ Standard Library?

c++ language-lawyer

In this lecture, the speaker mentions (at the beginning) that there are no pure virtual functions in the Standard Library (or he's not aware of any). I believe that Alex Stepanov was against this