Skip to content

Commit

Permalink
Support dark theme in WebView login
Browse files Browse the repository at this point in the history
- Only for Android 10 or higher
  • Loading branch information
WSTxda committed Mar 9, 2024
1 parent 8990922 commit 76d0869
Showing 1 changed file with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
Expand All @@ -30,6 +31,7 @@
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -43,7 +45,9 @@

import androidx.annotation.StringRes;
import androidx.preference.PreferenceManager;
import androidx.webkit.WebSettingsCompat;
import androidx.webkit.WebViewClientCompat;
import androidx.webkit.WebViewFeature;

import com.google.android.gms.R;

Expand Down Expand Up @@ -212,16 +216,23 @@ private void init() {
}

private static WebView createWebView(Context context) {
WebView webView = new WebView(context);
if (SDK_INT < LOLLIPOP) {
webView.setVisibility(VISIBLE);
} else {
webView.setVisibility(INVISIBLE);
}
webView.setLayoutParams(new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Context themedContext = new ContextThemeWrapper(context, R.style.LoginTheme);

WebView webView = new WebView(themedContext);
webView.setVisibility(View.INVISIBLE);
webView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
webView.setBackgroundColor(Color.TRANSPARENT);

boolean appIsDark = (themedContext.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;

if (Build.VERSION.SDK_INT >= 29) {
if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
WebSettingsCompat.setForceDark(webView.getSettings(), appIsDark ? WebSettingsCompat.FORCE_DARK_ON : WebSettingsCompat.FORCE_DARK_OFF);
}
}

prepareWebViewSettings(context, webView.getSettings());

return webView;
}

Expand Down

0 comments on commit 76d0869

Please sign in to comment.