Full size of an Android View being clipped by its parent

Ask by : manu3d November 05, 2012 11:25

Please take a look at this xml layout.

    <LinearLayout android:id="@+id/dataTableContainer"

        <LinearLayout android:id="@+id/dataTable"


At runtime the "dataTable" element is populated with children that are wider than what the "dataTableContainer" can display. However, as I inspect in eclipse the mMeasuredWidth of the two elements (or even the return value of getWidth()), they turn out to be exactly the same. This shouldn't be the case. My understanding is that the children of "dataTable" should push it to assume their own width given its "wrap_content" setting. Conversely, "dataTableContainer" should be smaller than that, given its "fill_parent" setting which is constrained by the screen size.

I've also tried with "clipChildren" set to false, just in case the width gets clipped by the container but the result remain the same: both table and container have the same width.

Can anybody explain what's going on?

To give you some context, what I'm trying to do is to gather the sizes of these elements to determine how much of the "dataTable" element lies -outside- of the container and therefore unseen. This in turn is needed to limit how far the user can drag the table element. ScrollView elements limit the motion natively, but as I'm using a LinearLayout I need to replicate that limiting functionality.

View original question

Answer by : manu3dNovember 05, 2012 11:25

It appears that I was too trusting of Eclipse. While debugging using a breakpoint to inspect the state of the app and the values of many available variables, I relied on a list of variables provided by eclipse which included mMeasuredWidth/mMeasuredHeight. Notice that these are not variables from my source code. Eclipse somehow found them by itself, inspecting the objects for me. I therefore assumed that somehow they indeed represented -the- measured width/height. My assumption was wrong however, if only because a view can be measured in different ways (i.e. using MeasureSpec.UNSPECIFIED or MeasureSpec.EXACTLY) and eclipse can't know which measure I needed.

I therefore triggered the measuring I needed by adding to my code:

int measureSpecParams = View.MeasureSpec.getSize(View.MeasureSpec.UNSPECIFIED);
dataTableView.measure(measureSpecParams, measureSpecParams);
int measuredWidth = dataTableView.getMeasuredWidth();
int measuredHeight = dataTableView.getMeasuredHeight();

This did give me the view sizes I was after, namely how big the "dataTable" view would be if the parent view nor the screen limited it.

View original answer