# Protection

# Protection

Artifact:[androidx.core:core](https://developer.android.com/jetpack/androidx/releases/core)  
[View Source](https://cs.android.com/search?q=file:androidx/core/view/insets/Protection.java+class:androidx.core.view.insets.Protection)  
Added in[1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/core/view/insets/Protection "View this page in Java")

<br />

```
abstract class Protection
```

<br />

Known direct subclasses  
[ColorProtection](https://developer.android.com/reference/kotlin/androidx/core/view/insets/ColorProtection),[GradientProtection](https://developer.android.com/reference/kotlin/androidx/core/view/insets/GradientProtection)  

|-------------------------------------------------------------------------------------------------------------------|------------------------------------------------------|
| [ColorProtection](https://developer.android.com/reference/kotlin/androidx/core/view/insets/ColorProtection)       | A type of protection which draws the plain color.    |
| [GradientProtection](https://developer.android.com/reference/kotlin/androidx/core/view/insets/GradientProtection) | A type of protection which draws the gradient color. |

*** ** * ** ***

An abstract class which describes a layer to be placed on the content of a window and underneath the system bar (which has a transparent background) to ensure the readability of the foreground elements (e.g., text, icons, ..., etc) of the system bar.

Concrete derived classes would describe how the protection should be drawn by supplying the[Drawable](https://developer.android.com/reference/android/graphics/drawable/Drawable.html).

The object of this class is stateful, and can only be used by one[ProtectionLayout](https://developer.android.com/reference/kotlin/androidx/core/view/insets/ProtectionLayout)at a time.  

|                                                     See also                                                      |
|-------------------------------------------------------------------------------------------------------------------|---|
| [ColorProtection](https://developer.android.com/reference/kotlin/androidx/core/view/insets/ColorProtection)       |   |
| [GradientProtection](https://developer.android.com/reference/kotlin/androidx/core/view/insets/GradientProtection) |   |
| [ProtectionLayout](https://developer.android.com/reference/kotlin/androidx/core/view/insets/ProtectionLayout)     |   |

## Summary

|                                                                                                                                                                       ### Public constructors                                                                                                                                                                       |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Protection](https://developer.android.com/reference/kotlin/androidx/core/view/insets/Protection#Protection(int))`(side: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Creates an instance associated with a[WindowInsetsCompat.Side](https://developer.android.com/reference/kotlin/androidx/core/view/WindowInsetsCompat.Side). |

|                                                                                          ### Public functions                                                                                          |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                          | [animateAlpha](https://developer.android.com/reference/kotlin/androidx/core/view/insets/Protection#animateAlpha(float))`(toAlpha: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` Animates the alpha from the current value to the specified one.                                                                                                                                                                                                                                                 |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                          | [animateInsetsAmount](https://developer.android.com/reference/kotlin/androidx/core/view/insets/Protection#animateInsetsAmount(float))`(toInsetsAmount: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` Animates the insets amount from the current value to the specified one.                                                                                                                                                                                                                    |
| `@`[FloatRange](https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange)`(from = 0.0, to = 1.0) `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | [getAlpha](https://developer.android.com/reference/kotlin/androidx/core/view/insets/Protection#getAlpha())`()` Gets the opacity of the protection.                                                                                                                                                                                                                                                                                                                                                                                    |
| [Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)                                                                                                                        | [getInsetAmount](https://developer.android.com/reference/kotlin/androidx/core/view/insets/Protection#getInsetAmount())`()` Gets the depth of the protection.                                                                                                                                                                                                                                                                                                                                                                          |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)                                                                                                                            | [getSide](https://developer.android.com/reference/kotlin/androidx/core/view/insets/Protection#getSide())`()` Gets the side of this protection.                                                                                                                                                                                                                                                                                                                                                                                        |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                          | [setAlpha](https://developer.android.com/reference/kotlin/androidx/core/view/insets/Protection#setAlpha(float))`(alpha: @`[FloatRange](https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange)`(from = 0.0, to = 1.0) `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` Sets the opacity of the protection to a value from 0 to 1, where 0 means the protection is completely transparent and 1 means the protection is completely opaque.                                   |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                                                                                          | [setInsetAmount](https://developer.android.com/reference/kotlin/androidx/core/view/insets/Protection#setInsetAmount(float))`(insetAmount: @`[FloatRange](https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange)`(from = 0.0, to = 1.0) `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` Sets the depth of the protection to a value from 0 to 1, where 0 means the protection is completely outside the window and 1 means the protection is completely inside the window. |

## Public constructors

### Protection

Added in[1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)  

```
Protection(side: Int)
```

Creates an instance associated with a[WindowInsetsCompat.Side](https://developer.android.com/reference/kotlin/androidx/core/view/WindowInsetsCompat.Side).  

|                                     Parameters                                      |
|-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------|
| `side: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the given[WindowInsetsCompat.Side](https://developer.android.com/reference/kotlin/androidx/core/view/WindowInsetsCompat.Side). |

|                                                        Throws                                                         |
|-----------------------------------------------------------------------------------------------------------------------|-------------------------------------------------|
| [java.lang.IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException.html) | if the given side is not one of the four sides. |

## Public functions

### animateAlpha

Added in[1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)  

```
fun animateAlpha(toAlpha: Float): Unit
```

Animates the alpha from the current value to the specified one.

Calling[setAlpha](https://developer.android.com/reference/kotlin/androidx/core/view/insets/Protection#setAlpha(float))during the animation will cancel the existing alpha animation.  

|                                         Parameters                                         |
|--------------------------------------------------------------------------------------------|------------------------------------------------------------|
| `toAlpha: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The alpha that will be animated to. The range is \[0, 1\]. |

### animateInsetsAmount

Added in[1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)  

```
fun animateInsetsAmount(toInsetsAmount: Float): Unit
```

Animates the insets amount from the current value to the specified one.

Calling[setInsetAmount](https://developer.android.com/reference/kotlin/androidx/core/view/insets/Protection#setInsetAmount(float))during the animation will cancel the existing animation of insets amount.  

|                                            Parameters                                             |
|---------------------------------------------------------------------------------------------------|--------------------------------------------------------------------|
| `toInsetsAmount: `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The insets amount that will be animated to. The range is \[0, 1\]. |

### getAlpha

Added in[1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)  

```
fun getAlpha(): @FloatRange(from = 0.0, to = 1.0) Float
```

Gets the opacity of the protection. This is a value from 0 to 1, where 0 means the protection is completely transparent and 1 means the protection is completely opaque.  

|                                                                                                Returns                                                                                                 |
|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------|
| `@`[FloatRange](https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange)`(from = 0.0, to = 1.0) `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The opacity of the protection. |

### getInsetAmount

Added in[1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)  

```
fun getInsetAmount(): Float
```

Gets the depth of the protection. This is a value from 0 to 1, where 0 means the protection completely inside the window and 1 means the protection is completely outside the window.  

|                                     Returns                                     |
|---------------------------------------------------------------------------------|------------------------------|
| [Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The depth of the protection. |

### getSide

Added in[1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)  

```
fun getSide(): Int
```

Gets the side of this protection.  

|                                   Returns                                   |
|-----------------------------------------------------------------------------|----------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | the side this protection is associated with. |

### setAlpha

Added in[1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)  

```
fun setAlpha(alpha: @FloatRange(from = 0.0, to = 1.0) Float): Unit
```

Sets the opacity of the protection to a value from 0 to 1, where 0 means the protection is completely transparent and 1 means the protection is completely opaque.  

|                                                                                                  Parameters                                                                                                   |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------|
| `alpha: @`[FloatRange](https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange)`(from = 0.0, to = 1.0) `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The opacity of the protection. |

|                                                        Throws                                                         |
|-----------------------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| [java.lang.IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException.html) | if the given alpha is not in a range of \[0, 1\]. |

### setInsetAmount

Added in[1.16.0](https://developer.android.com/jetpack/androidx/releases/core#1.16.0)  

```
fun setInsetAmount(insetAmount: @FloatRange(from = 0.0, to = 1.0) Float): Unit
```

Sets the depth of the protection to a value from 0 to 1, where 0 means the protection is completely outside the window and 1 means the protection is completely inside the window.  

|                                                                                                     Parameters                                                                                                      |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------|
| `insetAmount: @`[FloatRange](https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange)`(from = 0.0, to = 1.0) `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The depth of the protection. |

|                                                        Throws                                                         |
|-----------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------|
| [java.lang.IllegalArgumentException](https://developer.android.com/reference/java/lang/IllegalArgumentException.html) | if the given inset amount is not in a range of \[0, 1\]. |