Skip to content

Commit db94570

Browse files
committed
Add conditional layout to handle Android 15+ edge to edge layout default.
1 parent 2c4f703 commit db94570

1 file changed

Lines changed: 44 additions & 3 deletions

File tree

python-for-android/dists/kolibri/src/main/java/org/kivy/android/PythonActivity.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@
3232
import android.graphics.Color;
3333

3434
import android.widget.AbsoluteLayout;
35+
import android.widget.FrameLayout;
3536
import android.view.ViewGroup.LayoutParams;
37+
import android.view.WindowInsets;
38+
import android.view.View;
39+
import android.graphics.Insets;
3640

3741
import android.webkit.WebBackForwardList;
3842
import android.webkit.WebViewClient;
@@ -177,7 +181,6 @@ public void onClick(DialogInterface dialog,int id) {
177181
String unencodedHtml = PythonActivity.mActivity.getString(R.string.loading_page_html);
178182
String encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(), Base64.NO_PADDING);
179183
mWebView.loadData(encodedHtml, "text/html", "base64");
180-
mWebView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
181184
mWebView.setWebViewClient(new WebViewClient() {
182185
@Override
183186
public boolean shouldOverrideUrlLoading(WebView view, String url) {
@@ -200,8 +203,46 @@ public void onPageFinished(WebView view, String url) {
200203

201204
}
202205
});
203-
mLayout = new AbsoluteLayout(PythonActivity.mActivity);
204-
mLayout.addView(mWebView);
206+
207+
// Handle edge-to-edge insets only on Android 15+ (API 35+) where edge-to-edge is enforced
208+
if (android.os.Build.VERSION.SDK_INT >= 35) {
209+
// Use FrameLayout for Android 15+ to properly support margins for edge-to-edge
210+
mLayout = new FrameLayout(PythonActivity.mActivity);
211+
212+
// Set WebView with FrameLayout params that support margins
213+
FrameLayout.LayoutParams webViewParams = new FrameLayout.LayoutParams(
214+
FrameLayout.LayoutParams.MATCH_PARENT,
215+
FrameLayout.LayoutParams.MATCH_PARENT
216+
);
217+
mWebView.setLayoutParams(webViewParams);
218+
mLayout.addView(mWebView);
219+
220+
mLayout.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
221+
@Override
222+
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
223+
// Get system bar insets using the Android 11+ API
224+
android.graphics.Insets systemBarsInsets = insets.getInsets(WindowInsets.Type.systemBars());
225+
226+
// Apply margins to the WebView to avoid system bars
227+
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mWebView.getLayoutParams();
228+
params.leftMargin = systemBarsInsets.left;
229+
params.topMargin = systemBarsInsets.top;
230+
params.rightMargin = systemBarsInsets.right;
231+
params.bottomMargin = systemBarsInsets.bottom;
232+
mWebView.setLayoutParams(params);
233+
234+
// Return the insets unchanged to allow other views to also handle them
235+
return insets;
236+
}
237+
});
238+
// Enable edge-to-edge but handle insets properly
239+
mLayout.setFitsSystemWindows(false);
240+
} else {
241+
// For older Android versions, use the original AbsoluteLayout setup
242+
mLayout = new AbsoluteLayout(PythonActivity.mActivity);
243+
mWebView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
244+
mLayout.addView(mWebView);
245+
}
205246

206247
setContentView(mLayout);
207248

0 commit comments

Comments
 (0)