Package 

Class PercentLayoutHelper


  • 
    public class PercentLayoutHelper
    
                        

    Helper for layouts that want to support percentage based dimensions.

    This class collects utility methods that are involved in extracting percentage based dimension attributes and applying them to ViewGroup's children. If you would like to implement a layout that supports percentage based dimensions, you need to take several steps:

    • You need a ViewGroup.LayoutParams subclass in your ViewGroup that implements PercentLayoutParams.
    • In your {@code LayoutParams(Context c, AttributeSet attrs)} constructor create an instance of PercentLayoutInfo. Return this object from {@code public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo()} method that you implemented for PercentLayoutParams interface.
    • Override setBaseAttributes with a single line implementation {@code PercentLayoutHelper.fetchWidthAndHeight(this, a, * widthAttr, heightAttr);}
    • In your ViewGroup override generateLayoutParams to return your LayoutParams.
    • In your onMeasure override, you need to implement following pattern:
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      if (mHelper.handleMeasuredStateTooSmall()) {
      super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      }
      }
      
    • In your onLayout override, you need to implement following pattern:
      protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
      super.onLayout(changed, left, top, right, bottom);
      mHelper.restoreOriginalParams();
      }
      
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      public class PercentLayoutHelper.PercentLayoutInfo

      Container for information about percentage dimensions and margins. It acts as an extensionfor {@code LayoutParams}.

      public interface PercentLayoutHelper.PercentLayoutParams

      If a layout wants to support percentage based dimensions and use this helper class, its {@code LayoutParams} subclass must implement this interface.

      Your {@code LayoutParams} subclass should contain an instance of {@code PercentLayoutInfo} and the implementation of this interface should be a simple accessor.

    • Method Summary

      Modifier and Type Method Description
      static void fetchWidthAndHeight(ViewGroup.LayoutParams params, TypedArray array, int widthAttr, int heightAttr) Helper method to be called from setBaseAttributes overridethat reads layout_width and layout_height attribute values without throwing an exception ifthey aren't present.
      void adjustChildren(int widthMeasureSpec, int heightMeasureSpec) Iterates over children and changes their width and height to one calculated from percentagevalues.
      void restoreOriginalParams() Iterates over children and restores their original dimensions that were changed forpercentage values.
      boolean handleMeasuredStateTooSmall() Iterates over children and checks if any of them would like to get more space than itreceived through the percentage dimension.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • PercentLayoutHelper

        PercentLayoutHelper(ViewGroup host)
    • Method Detail

      • adjustChildren

         void adjustChildren(int widthMeasureSpec, int heightMeasureSpec)

        Iterates over children and changes their width and height to one calculated from percentagevalues.

        Parameters:
        widthMeasureSpec - Width MeasureSpec of the parent ViewGroup.
        heightMeasureSpec - Height MeasureSpec of the parent ViewGroup.
      • restoreOriginalParams

         void restoreOriginalParams()

        Iterates over children and restores their original dimensions that were changed forpercentage values. Calling this method only makes sense if you previously called adjustChildren.

      • handleMeasuredStateTooSmall

         boolean handleMeasuredStateTooSmall()

        Iterates over children and checks if any of them would like to get more space than itreceived through the percentage dimension.

        If you are building a layout that supports percentage dimensions you are encouraged to takeadvantage of this method. The developer should be able to specify that a child should beremeasured by adding normal dimension attribute with {@code wrap_content} value. For examplehe might specify child's attributes as {@code app:layout_widthPercent="60%p"} and {@code android:layout_width="wrap_content"}. In this case if the child receives too littlespace, it will be remeasured with width set to {@code WRAP_CONTENT}.