Some extensions to help developing a foldable compatible application and a FoldableActivity for simple cases
First you need to add the AndroidX Window component
implementation "androidx.window:window:$versions.androidx.window"
Then add the Foldable implementation from arch-toolkit
implementation "io.github.matheus-corregiari:foldable:$versions.arch.foldable"
class FoldableSampleActivity : FoldableActivity() {
override val orientation: FoldingFeature.Orientation = FoldingFeature.Orientation.HORIZONTAL
override val topViewId: Int = R.layout.activity_foldable_top_sample
override val bottomViewId: Int = R.layout.activity_foldable_bottom_sample
}
class FoldableSampleActivity : FoldableActivity() {
override val orientation: FoldingFeature.Orientation = FoldingFeature.Orientation.VERTICAL
override val startViewId: Int = R.layout.activity_foldable_start_sample
override val endViewId: Int = R.layout.activity_foldable_end_sample
}
class FoldableSampleActivity : AppCompatActivity(R.layout.activity_foldable_sample) {
private val root: ViewGroup by viewProvider(R.id.root_layout)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handleFoldableStateChange(
layout = root,
reactiveGuideId = R.id.fold_guideline,
orientation = FoldingFeature.Orientation.VERTICAL
)
}
}
class FoldableSampleActivity :
AppCompatActivity(R.layout.activity_foldable_sample),
OnFoldableStateChangeListener {
private val root: ViewGroup by viewProvider(R.id.root_layout)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
observeFoldableStateChanges(
layout = root,
orientation = FoldingFeature.Orientation.VERTICAL,
listener = this
)
}
override fun onChangeState(isFolded: Boolean) {
//Called whenever there is ANY state changes (called if state or rotation changes)
}
override fun onOpenFlat(foldPosition: Int, orientation: FoldingFeature.Orientation) {
//Called whenever the foldable device is fully open (called if state or rotation changes)
}
override fun onHalfOpen(foldPosition: Int, orientation: FoldingFeature.Orientation) {
//Called whenever the foldable device is half open (called if state or rotation changes)
}
override fun onClosed() {
//Called whenever the foldable device is closed (called if state or rotation changes)
}
override fun onWrongOrientation() {
//Called whenever the foldable device rotates and the orientation is the chosen one
}
}
class FoldableSampleActivity :
AppCompatActivity(R.layout.activity_foldable_sample),
OnFoldableStateChangeListener {
private val root: ViewGroup by viewProvider(R.id.root_layout)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
onNewLayoutInfo { info: WindowLayoutInfo ->
//Called whenever there is a rotation or state change
}
}
}