@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		final int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);

		// increment the x position as we progress through a line
		int xpos = getPaddingLeft();
		// increment the y position as we progress through the lines
		int ypos = getPaddingTop();
		// the height of the current line
		int line_height = 0;

		// go through children
		// to work out the height required for this view
		measureChildren(widthMeasureSpec, heightMeasureSpec);

		View child;
		MarginLayoutParams childMarginLayoutParams;
		int childWidth, childHeight, childMarginLeft, childMarginRight, childMarginTop, childMarginBottom;

		for (int i = 0; i < getChildCount(); i++) {
			child = getChildAt(i);

			if (child.getVisibility() != GONE) {
				childWidth = child.getMeasuredWidth();
				childHeight = child.getMeasuredHeight();

				if (child.getLayoutParams() != null
						&& child.getLayoutParams() instanceof MarginLayoutParams) {
					childMarginLayoutParams = (MarginLayoutParams) child
							.getLayoutParams();

					childMarginLeft = childMarginLayoutParams.leftMargin;
					childMarginRight = childMarginLayoutParams.rightMargin;
					childMarginTop = childMarginLayoutParams.topMargin;
					childMarginBottom = childMarginLayoutParams.bottomMargin;
				} else {
					childMarginLeft = 0;
					childMarginRight = 0;
					childMarginTop = 0;
					childMarginBottom = 0;
				}

				if (xpos + childMarginLeft + childWidth + childMarginRight
						+ getPaddingRight() > width) {
					// this child will need to go on a new line

					xpos = getPaddingLeft();
					ypos += line_height;

					line_height = childMarginTop + childHeight
							+ childMarginBottom;
				} else
					// enough space for this child on the current line
					line_height = Math.max(line_height, childMarginTop
							+ childHeight + childMarginBottom);

				xpos += childMarginLeft + childWidth + childMarginRight;
			}
		}

		ypos += line_height + getPaddingBottom();

		if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED)
			// set height as measured since there's no height restrictions
			height = ypos;
		else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST
				&& ypos < height)
			// set height as measured since it's less than the maximum allowed
			height = ypos;

		setMeasuredDimension(width, height);
	}
@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		final int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);

		
		int xpos = getPaddingLeft();
		
		int ypos = getPaddingTop();
		
		int line_height = 0;

		
		
		measureChildren(widthMeasureSpec, heightMeasureSpec);

		View child;
		MarginLayoutParams childMarginLayoutParams;
		int childWidth, childHeight, childMarginLeft, childMarginRight, childMarginTop, childMarginBottom;

		for (int i = 0; i < getChildCount(); i++) {
			child = getChildAt(i);

			if (child.getVisibility() != GONE) {
				childWidth = child.getMeasuredWidth();
				childHeight = child.getMeasuredHeight();

				if (child.getLayoutParams() != null
						&& child.getLayoutParams() instanceof MarginLayoutParams) {
					childMarginLayoutParams = (MarginLayoutParams) child
							.getLayoutParams();

					childMarginLeft = childMarginLayoutParams.leftMargin;
					childMarginRight = childMarginLayoutParams.rightMargin;
					childMarginTop = childMarginLayoutParams.topMargin;
					childMarginBottom = childMarginLayoutParams.bottomMargin;
				} else {
					childMarginLeft = 0;
					childMarginRight = 0;
					childMarginTop = 0;
					childMarginBottom = 0;
				}

				if (xpos + childMarginLeft + childWidth + childMarginRight
						+ getPaddingRight() > width) {
					

					xpos = getPaddingLeft();
					ypos += line_height;

					line_height = childMarginTop + childHeight
							+ childMarginBottom;
				} else
					
					line_height = Math.max(line_height, childMarginTop
							+ childHeight + childMarginBottom);

				xpos += childMarginLeft + childWidth + childMarginRight;
			}
		}

		ypos += line_height + getPaddingBottom();

		if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.UNSPECIFIED)
			
			height = ypos;
		else if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.AT_MOST
				&& ypos < height)
			
			height = ypos;

		setMeasuredDimension(width, height);
	}
@ Override protected void onMeasure ( int widthMeasureSpec , int heightMeasureSpec ) { super . onMeasure ( widthMeasureSpec , heightMeasureSpec ) ; final int width = MeasureSpec . getSize ( widthMeasureSpec ) ; int height = MeasureSpec . getSize ( heightMeasureSpec ) ; // increment the x position as we progress through a line int xpos = getPaddingLeft ( ) ; // increment the y position as we progress through the lines int ypos = getPaddingTop ( ) ; // the height of the current line int line_height = 0 ; // go through children // to work out the height required for this view measureChildren ( widthMeasureSpec , heightMeasureSpec ) ; View child ; MarginLayoutParams childMarginLayoutParams ; int childWidth , childHeight , childMarginLeft , childMarginRight , childMarginTop , childMarginBottom ; for ( int i = 0 ; i < getChildCount ( ) ; i ++ ) { child = getChildAt ( i ) ; if ( child . getVisibility ( ) != GONE ) { childWidth = child . getMeasuredWidth ( ) ; childHeight = child . getMeasuredHeight ( ) ; if ( child . getLayoutParams ( ) != null && child . getLayoutParams ( ) instanceof MarginLayoutParams ) { childMarginLayoutParams = ( MarginLayoutParams ) child . getLayoutParams ( ) ; childMarginLeft = childMarginLayoutParams . leftMargin ; childMarginRight = childMarginLayoutParams . rightMargin ; childMarginTop = childMarginLayoutParams . topMargin ; childMarginBottom = childMarginLayoutParams . bottomMargin ; } else { childMarginLeft = 0 ; childMarginRight = 0 ; childMarginTop = 0 ; childMarginBottom = 0 ; } if ( xpos + childMarginLeft + childWidth + childMarginRight + getPaddingRight ( ) > width ) { // this child will need to go on a new line xpos = getPaddingLeft ( ) ; ypos += line_height ; line_height = childMarginTop + childHeight + childMarginBottom ; } else // enough space for this child on the current line line_height = Math . max ( line_height , childMarginTop + childHeight + childMarginBottom ) ; xpos += childMarginLeft + childWidth + childMarginRight ; } } ypos += line_height + getPaddingBottom ( ) ; if ( MeasureSpec . getMode ( heightMeasureSpec ) == MeasureSpec . UNSPECIFIED ) // set height as measured since there's no height restrictions height = ypos ; else if ( MeasureSpec . getMode ( heightMeasureSpec ) == MeasureSpec . AT_MOST && ypos < height ) // set height as measured since it's less than the maximum allowed height = ypos ; setMeasuredDimension ( width , height ) ; }
from http://adilatwork.blogspot.it/2012/12/android-horizontal-flow-layout.html
from http : // adilatwork . blogspot . it / 2012 / 12 / android - horizontal - flow - layout . html