Skip to content

Commit 4759fad

Browse files
committed
Refactor: Update edge-to-edge implementation for Android 15
This commit updates the `applyEdgeToEdge` logic in `BaseActivity` to target Android 15 (Vanilla Ice Cream) and above. Key changes include: - Restricting `setDecorFitsSystemWindows` and inset listeners to `SDK_INT >= VANILLA_ICE_CREAM`. - Switching the inset listener from the content view to the `decorView`. - Accounting for IME (keyboard) insets when calculating bottom padding. - Updating padding logic to include left and right system bar insets.
1 parent 26a4334 commit 4759fad

File tree

1 file changed

+23
-16
lines changed

1 file changed

+23
-16
lines changed
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.ahmadabuhasan.qrbarcode.utils
22

3+
import android.os.Build
34
import android.os.Bundle
45
import android.view.View
56
import androidx.appcompat.app.AppCompatActivity
@@ -10,27 +11,33 @@ import androidx.core.view.WindowInsetsCompat
1011
abstract class BaseActivity : AppCompatActivity() {
1112

1213
override fun onCreate(savedInstanceState: Bundle?) {
13-
WindowCompat.setDecorFitsSystemWindows(window, false)
14-
applyEdgeToEdge()
1514
super.onCreate(savedInstanceState)
15+
applyEdgeToEdge()
1616
}
1717

1818
private fun applyEdgeToEdge() {
19-
val content = findViewById<View>(android.R.id.content)
20-
21-
ViewCompat.setOnApplyWindowInsetsListener(content) { view, insets ->
22-
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
23-
24-
view.setPadding(
25-
view.paddingLeft,
26-
systemBars.top,
27-
view.paddingRight,
28-
systemBars.bottom
29-
)
30-
31-
insets
19+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
20+
WindowCompat.setDecorFitsSystemWindows(window, false)
21+
22+
val decorView = window.decorView
23+
val content = findViewById<View>(android.R.id.content)
24+
25+
decorView.post {
26+
ViewCompat.setOnApplyWindowInsetsListener(decorView) { _, insets ->
27+
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
28+
val ime = insets.getInsets(WindowInsetsCompat.Type.ime())
29+
30+
content.setPadding(
31+
systemBars.left,
32+
systemBars.top,
33+
systemBars.right,
34+
maxOf(ime.bottom, systemBars.bottom)
35+
)
36+
37+
insets
38+
}
39+
}
3240
}
33-
3441
}
3542

3643
}

0 commit comments

Comments
 (0)