1

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" />
2

4 Answers 4

4

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

1
  • Thanks @Ajaya Tiwari, You save my day, it's working fine thanks a lot. Sep 5, 2019 at 8:08
0

in activity have webView

 @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    init here language again  then 
    setContentView(R.layout.activity_terms_of_use);
0
0

After a lot of testing and following the advices from the other answers, I've finally fixed the issue.

Issues with WebView:

  • WebView have to be loaded dynamically in the activity. Loading from XML file via Activity#setContentView() won't work for the first time and localisation of the entire activity will break. However, subsequent launches or Activity#recreate() will work as expected.
  • Even if WebView is loaded dynamically, it won't work for the first time. Again, subsequent launches or Activity#recreate() will work properly.

Considering the issues above, the solution involves loading WebView dynamically and then, the immediate recreation of the activity.

ActivityWithWebView.java

public class ActivityWithWebView extends AppCompatActivity {
    private WebView webView;
    private static boolean firstTime = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_with_web_view);
        setSupportActionBar(findViewById(R.id.toolbar));
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) actionBar.setTitle(R.string.instructions);
        // WebView has to be loaded dynamically to prevent in-app localisation issue.
        webView = new WebView(this);
        if (firstTime) {
            // Recreate if loaded for the first time to prevent localisation issue.
            recreate();
            firstTime = false;
            return;
        }
        LinearLayoutCompat webviewWrapper = findViewById(R.id.webview_wrapper);
        webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        webviewWrapper.addView(webView);
        // Do other works.
    }
}

activity_with_web_view.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?android:colorBackground" />

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/webview_wrapper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

0

If you are using Jetpack composes' Accompanist WebView same thing will happen, the same way I solved it was to set the language again in the param

WebView(
      onCreated = { //here set the language again }
)

hope this will help someone.

New contributor
Abubeker Mohammed is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.