22
33import android .graphics .Color ;
44import android .os .Build ;
5+ import android .view .Gravity ;
56import android .view .View ;
67import android .view .ViewGroup ;
8+ import android .widget .FrameLayout ;
79import androidx .annotation .NonNull ;
10+ import androidx .annotation .Nullable ;
811import androidx .core .graphics .Insets ;
912import androidx .core .view .ViewCompat ;
1013import androidx .core .view .WindowInsetsCompat ;
1114import com .getcapacitor .Logger ;
15+ import java .lang .reflect .Constructor ;
1216
1317public class EdgeToEdge {
1418
@@ -18,11 +22,21 @@ public class EdgeToEdge {
1822 @ NonNull
1923 private final EdgeToEdgePlugin plugin ;
2024
25+ @ Nullable
26+ private View navigationBarOverlay ;
27+
28+ @ Nullable
29+ private View statusBarOverlay ;
30+
2131 public EdgeToEdge (@ NonNull EdgeToEdgePlugin plugin , @ NonNull EdgeToEdgeConfig config ) {
2232 this .config = config ;
2333 this .plugin = plugin ;
24- // Apply insets to disable the edge-to-edge feature
25- setBackgroundColor (config .getBackgroundColor ());
34+ // Create color overlays
35+ createColorOverlays ();
36+ // Set colors from config
37+ setStatusBarColor (config .getStatusBarColor ());
38+ setNavigationBarColor (config .getNavigationBarColor ());
39+ // Apply insets to enable the edge-to-edge feature
2640 applyInsets ();
2741 }
2842
@@ -40,7 +54,17 @@ public ViewGroup.MarginLayoutParams getInsets() {
4054 }
4155
4256 public void setBackgroundColor (String color ) {
43- setBackgroundColor (Color .parseColor (color ));
57+ int parsedColor = Color .parseColor (color );
58+ setStatusBarColor (parsedColor );
59+ setNavigationBarColor (parsedColor );
60+ }
61+
62+ public void setNavigationBarColor (String color ) {
63+ setNavigationBarColor (Color .parseColor (color ));
64+ }
65+
66+ public void setStatusBarColor (String color ) {
67+ setStatusBarColor (Color .parseColor (color ));
4468 }
4569
4670 private void applyInsets () {
@@ -64,6 +88,9 @@ private void applyInsets() {
6488 mlp .rightMargin = systemBarsInsets .right ;
6589
6690 view .setLayoutParams (mlp );
91+
92+ // Update color overlays based on current insets
93+ updateColorOverlays (systemBarsInsets );
6794 }
6895 // Set listener
6996 ViewCompat .setOnApplyWindowInsetsListener (view , (v , windowInsets ) -> {
@@ -87,6 +114,9 @@ private void applyInsets() {
87114
88115 v .setLayoutParams (mlp );
89116
117+ // Update color overlays based on current insets
118+ updateColorOverlays (systemBarsInsets );
119+
90120 return WindowInsetsCompat .CONSUMED ;
91121 });
92122 }
@@ -104,13 +134,105 @@ private void removeInsets() {
104134 view .setLayoutParams (mlp );
105135 // Reset listener
106136 ViewCompat .setOnApplyWindowInsetsListener (view , null );
137+ // Remove color overlays
138+ removeColorOverlays ();
107139 }
108140
109- private void setBackgroundColor (int color ) {
110- View view = plugin .getBridge ().getWebView ();
111- // Get parent view
112- ViewGroup parent = (ViewGroup ) view .getParent ();
113- // Set background color
114- parent .setBackgroundColor (color );
141+ private void createColorOverlays () {
142+ View webView = plugin .getBridge ().getWebView ();
143+ ViewGroup parent = (ViewGroup ) webView .getParent ();
144+
145+ if (statusBarOverlay == null ) {
146+ statusBarOverlay = new View (parent .getContext ());
147+ parent .addView (statusBarOverlay , 0 ); // Add behind webview
148+ }
149+
150+ if (navigationBarOverlay == null ) {
151+ navigationBarOverlay = new View (parent .getContext ());
152+ parent .addView (navigationBarOverlay , 0 ); // Add behind webview
153+ }
154+ }
155+
156+ private void removeColorOverlays () {
157+ View webView = plugin .getBridge ().getWebView ();
158+ ViewGroup parent = (ViewGroup ) webView .getParent ();
159+
160+ if (statusBarOverlay != null ) {
161+ parent .removeView (statusBarOverlay );
162+ statusBarOverlay = null ;
163+ }
164+
165+ if (navigationBarOverlay != null ) {
166+ parent .removeView (navigationBarOverlay );
167+ navigationBarOverlay = null ;
168+ }
169+ }
170+
171+ private void setNavigationBarColor (int color ) {
172+ if (navigationBarOverlay != null ) {
173+ navigationBarOverlay .setBackgroundColor (color );
174+ }
175+ }
176+
177+ private void setStatusBarColor (int color ) {
178+ if (statusBarOverlay != null ) {
179+ statusBarOverlay .setBackgroundColor (color );
180+ }
181+ }
182+
183+ private void updateColorOverlays (Insets systemBarsInsets ) {
184+ View webView = plugin .getBridge ().getWebView ();
185+ ViewGroup parent = (ViewGroup ) webView .getParent ();
186+
187+ if (statusBarOverlay != null ) {
188+ // Position status bar overlay at top
189+ ViewGroup .LayoutParams statusParams = createLayoutParams (
190+ parent ,
191+ ViewGroup .LayoutParams .MATCH_PARENT ,
192+ systemBarsInsets .top ,
193+ Gravity .TOP
194+ );
195+ statusBarOverlay .setLayoutParams (statusParams );
196+ }
197+
198+ if (navigationBarOverlay != null ) {
199+ // Position navigation bar overlay at bottom
200+ ViewGroup .LayoutParams navParams = createLayoutParams (
201+ parent ,
202+ ViewGroup .LayoutParams .MATCH_PARENT ,
203+ systemBarsInsets .bottom ,
204+ Gravity .BOTTOM
205+ );
206+ navigationBarOverlay .setLayoutParams (navParams );
207+ }
208+ }
209+
210+ private ViewGroup .LayoutParams createLayoutParams (ViewGroup parent , int width , int height , int gravity ) {
211+ String parentClassName = parent .getClass ().getName ();
212+
213+ // Handle CoordinatorLayout using reflection
214+ if (parentClassName .contains ("CoordinatorLayout" )) {
215+ try {
216+ Class <?> layoutParamsClass = Class .forName ("androidx.coordinatorlayout.widget.CoordinatorLayout$LayoutParams" );
217+ Constructor <?> constructor = layoutParamsClass .getConstructor (int .class , int .class );
218+ ViewGroup .LayoutParams params = (ViewGroup .LayoutParams ) constructor .newInstance (width , height );
219+ // Set gravity using reflection
220+ layoutParamsClass .getField ("gravity" ).setInt (params , gravity );
221+ return params ;
222+ } catch (Exception e ) {
223+ Logger .error ("EdgeToEdge" , "Failed to create CoordinatorLayout.LayoutParams" , e );
224+ }
225+ }
226+
227+ // Handle FrameLayout
228+ if (parent instanceof FrameLayout ) {
229+ FrameLayout .LayoutParams params = new FrameLayout .LayoutParams (width , height );
230+ params .gravity = gravity ;
231+ return params ;
232+ }
233+
234+ // Fallback to MarginLayoutParams
235+ ViewGroup .MarginLayoutParams params = new ViewGroup .MarginLayoutParams (width , height );
236+ return params ;
115237 }
116238}
0 commit comments