Search code examples
androidandroid-studioandroid-layoutandroid-fragmentsandroid-webview

Using a webview changed default language


My app default language is Spanish (es). I have introduced a webview in XML. Prior to this change, the language is Spanish. After adding the webview, it's automatically showing English. How can I fix this issue or problem?

Thanks for helping in advance. I already used below code too.

WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUserAgentString(String.valueOf(Locale.SPANISH));
<WebView
   android:id="@+id/webView"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_marginStart="-5dp"
   tools:ignore="WebViewLayout" />

Solution

  • You can dynamically add web view and initialize the language again after that.

        llDynemic=(LinearLayout)findViewById(R.id.test);
    
        WebView webView = new WebView(getContext());// webview in mainactivity
                webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
                webView.setBackgroundColor(Color.TRANSPARENT);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.loadDataWithBaseURL(null, "<style>img{display: inline;height: auto;max-width: 100%;} a {color: #337ab7;}</style>" + newBody, "text/html", "UTF-8", null);
                llDynemic.addView(webView);
    
    // initialize the language here
    

    and it will work