# PercentLayoutHelper

Artifact: [androidx.percentlayout:percentlayout](https://developer.android.com/jetpack/androidx/releases/percentlayout) [View Source](https://cs.android.com/search?q=file:androidx/percentlayout/widget/PercentLayoutHelper.java+class:androidx.percentlayout.widget.PercentLayoutHelper) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0) Deprecated in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0)

*** ** * ** ***


```
class PercentLayoutHelper
```

<br />

*** ** * ** ***

> [!CAUTION]
> **This class is deprecated.**   
>
> consider using ConstraintLayout and associated layouts instead. The following shows how to replicate the functionality of percentage layouts with a ConstraintLayout. The Guidelines are used to define each percentage break point, and then a Button view is stretched to fill the gap:
>
> ```kotlin
> <androidx.constraintlayout.widget.ConstraintLayout        xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:app="http://schemas.android.com/apk/res-auto"        android:layout_width="match_parent"        android:layout_height="match_parent">    <androidx.constraintlayout.widget.Guideline        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/left_guideline"        app:layout_constraintGuide_percent=".15"        android:orientation="vertical"/>    <androidx.constraintlayout.widget.Guideline        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/right_guideline"        app:layout_constraintGuide_percent=".85"        android:orientation="vertical"/>    <androidx.constraintlayout.widget.Guideline        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/top_guideline"        app:layout_constraintGuide_percent=".15"        android:orientation="horizontal"/>    <androidx.constraintlayout.widget.Guideline        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/bottom_guideline"        app:layout_constraintGuide_percent=".85"        android:orientation="horizontal"/>    <Button        android:text="Button"        android:layout_width="0dp"        android:layout_height="0dp"        android:id="@+id/button"        app:layout_constraintLeft_toLeftOf="@+id/left_guideline"        app:layout_constraintRight_toRightOf="@+id/right_guideline"        app:layout_constraintTop_toTopOf="@+id/top_guideline"        app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline" /></androidx.constraintlayout.widget.ConstraintLayout>
> ```

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:

1. You need a `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html` subclass in your ViewGroup that implements `https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper.PercentLayoutParams`.
2. In your `LayoutParams(Context c, AttributeSet attrs)` constructor create an instance of `https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper.PercentLayoutInfo` by calling `https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#getPercentLayoutInfo(android.content.Context,android.util.AttributeSet)`. Return this object from `public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo()` method that you implemented for `https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper.PercentLayoutParams` interface.
3. Override `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#setBaseAttributes(android.content.res.TypedArray, int, int)` with a single line implementation `PercentLayoutHelper.fetchWidthAndHeight(this, a,
   widthAttr, heightAttr);`
4. In your ViewGroup override `https://developer.android.com/reference/android/view/ViewGroup.html#generateLayoutParams(android.util.AttributeSet)` to return your LayoutParams.
5. In your `https://developer.android.com/reference/android/view/View.html#onMeasure(int, int)` override, you need to implement following pattern:

   ```kotlin
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
       mHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
       super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       if (mHelper.handleMeasuredStateTooSmall()) {
           super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       }
   }
   ```
6. In your `https://developer.android.com/reference/android/view/ViewGroup.html#onLayout(boolean, int, int, int, int)` override, you need to implement following pattern:

   ```kotlin
   protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
       super.onLayout(changed, left, top, right, bottom);
       mHelper.restoreOriginalParams();
   }
   ```

## Summary

| ### Nested types |
|---|
| `class https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper.PercentLayoutInfo` **This class is deprecated.** use ConstraintLayout and Guidelines for layout support. <br /> |
| `interface https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper.PercentLayoutParams` **This interface is deprecated.** this class is deprecated along with its parent class. <br /> |

| ### Public constructors |
|---|
| `[PercentLayoutHelper](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#PercentLayoutHelper(android.view.ViewGroup))(host: https://developer.android.com/reference/android/view/ViewGroup.html)` **This function is deprecated.** Deprecated in Java |

| ### Public functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[adjustChildren](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#adjustChildren(int,int))(widthMeasureSpec: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, heightMeasureSpec: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` **This function is deprecated.** Deprecated in Java |
| `java-static https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[fetchWidthAndHeight](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#fetchWidthAndHeight(android.view.ViewGroup.LayoutParams,android.content.res.TypedArray,int,int))( params: https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html!, array: https://developer.android.com/reference/android/content/res/TypedArray.html!, widthAttr: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, heightAttr: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` **This function is deprecated.** Deprecated in Java |
| `java-static https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper.PercentLayoutInfo!` | `[getPercentLayoutInfo](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#getPercentLayoutInfo(android.content.Context,android.util.AttributeSet))(context: https://developer.android.com/reference/android/content/Context.html!, attrs: https://developer.android.com/reference/android/util/AttributeSet.html!)` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `[handleMeasuredStateTooSmall](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#handleMeasuredStateTooSmall())()` **This function is deprecated.** Deprecated in Java |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[restoreOriginalParams](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#restoreOriginalParams())()` **This function is deprecated.** Deprecated in Java |

## Public constructors

### PercentLayoutHelper

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0) Deprecated in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0)

```
[PercentLayoutHelper](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#PercentLayoutHelper(android.view.ViewGroup))(host: ViewGroup)
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

## Public functions

### adjustChildren

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0) Deprecated in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0)

```
fun [adjustChildren](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#adjustChildren(int,int))(widthMeasureSpec: Int, heightMeasureSpec: Int): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

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

| Parameters |
|---|---|
| `widthMeasureSpec: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Width MeasureSpec of the parent ViewGroup. |
| `heightMeasureSpec: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | Height MeasureSpec of the parent ViewGroup. |

### fetchWidthAndHeight

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0) Deprecated in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0)

```
java-static fun [fetchWidthAndHeight](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#fetchWidthAndHeight(android.view.ViewGroup.LayoutParams,android.content.res.TypedArray,int,int))(
    params: ViewGroup.LayoutParams!,
    array: TypedArray!,
    widthAttr: Int,
    heightAttr: Int
): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Helper method to be called from `https://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html#setBaseAttributes(android.content.res.TypedArray, int, int)` override that reads layout_width and layout_height attribute values without throwing an exception if they aren't present.

### getPercentLayoutInfo

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0) Deprecated in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0)

```
java-static fun [getPercentLayoutInfo](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#getPercentLayoutInfo(android.content.Context,android.util.AttributeSet))(context: Context!, attrs: AttributeSet!): PercentLayoutHelper.PercentLayoutInfo!
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Constructs a PercentLayoutInfo from attributes associated with a View. Call this method from `LayoutParams(Context c, AttributeSet attrs)` constructor.

### handleMeasuredStateTooSmall

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0) Deprecated in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0)

```
fun [handleMeasuredStateTooSmall](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#handleMeasuredStateTooSmall())(): Boolean
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Iterates over children and checks if any of them would like to get more space than it received through the percentage dimension. If you are building a layout that supports percentage dimensions you are encouraged to take advantage of this method. The developer should be able to specify that a child should be remeasured by adding normal dimension attribute with `wrap_content` value. For example he might specify child's attributes as `app:layout_widthPercent="60%p"` and `android:layout_width="wrap_content"`. In this case if the child receives too little space, it will be remeasured with width set to `WRAP_CONTENT`.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | True if the measure phase needs to be rerun because one of the children would like to receive more space. |

### restoreOriginalParams

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0) Deprecated in [1.0.0](https://developer.android.com/jetpack/androidx/releases/percentlayout#1.0.0)

```
fun [restoreOriginalParams](https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#restoreOriginalParams())(): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Deprecated in Java

Iterates over children and restores their original dimensions that were changed for percentage values. Calling this method only makes sense if you previously called `https://developer.android.com/reference/kotlin/androidx/percentlayout/widget/PercentLayoutHelper#adjustChildren(int,int)`.