I'm trying to create something similar to the home screen layout. This consists of multiple vertically oriented LinearLayouts within a single horizontally oriented LinearLayout, all housed in a HorizontalScrollView. I've written it as a custom class called 'HomeLayout' extending HorizontalScrollView.
LinearLayout wrapper = new LinearLayout(getContext());
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
wrapper.setLayoutParams(params);
wrapper.setOrientation(LinearLayout.HORIZONTAL);
addView(wrapper);
for(int i = 0; i < 2; i++) {
LinearLayout linear = (LinearLayout) View.inflate(getContext(), R.layout.screens, null);
linear.setLayoutParams(params);
wrapper.addView(linear);
}
The problem is 'screen2' has width '0' when added.
This only works if I call this in onDraw() and use getMeasuredWidth() and getMeasuredHeight() instead of LayoutParams.FILL_PARENT. And even then, I'm not sure if that's the right thing to do.
Furthermore I'm unable to reference the views 'wrapper', 'screen1', 'screen2' in onCreate().
I loosely followed this link: http://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/
What am I doing wrong?
Hoping this helps others. I solved my problem by implementing onMeasure() in the following way:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
for(int i = 0; i < mWrapper.getChildCount(); i++) {
View child = mWrapper.getChildAt(i);
child.setLayoutParams(new LinearLayout.LayoutParams(width,
LayoutParams.FILL_PARENT));
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}