TransWikia.com

i converted website into app using webview. but website background is not appearing in my app

Stack Overflow Asked by Neha Bhosale on January 13, 2021

<WebView
    android:id="@+id/webView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:scrollbars="none"
    />
</androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
    private WebView mywebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebview = (WebView) findViewById(R.id.webView);
        mywebview.setWebViewClient(new WebViewClient());
        mywebview.setBackgroundResource(R.drawable.wordpress);
        mywebview.setBackgroundColor(Color.TRANSPARENT);
        WebSettings webSettings = mywebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mywebview.loadUrl("https://www.indianfests.com/");
    }
    @Override
    public void onBackPressed() {
        if (mywebview.canGoBack()) {
            mywebview.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

2 Answers

If you mean the background image with the elephant statue that we see on https://www.indianfests.com/ is not loading: this is probably because Android requires all requests to use HTTPS by default from target API level 28 onwards.

The background image, however, is specified as plain HTTP: http://www.indianfests.com/wp-content/uploads/2020/08/wordpress.jpg. The server immediately redirects it to HTTPS, but because the HTTP request is blocked, the WebView cannot know that (and even if it did, it would be insecure to use it anyway). You can also see this in a regular web browser, which should show some kind of warning text or icon in the address bar.

So the best way to fix this is to simply change the website to use HTTPS throughout.

If you absolutely, positively cannot do that, you can use <application android:usesClearTextTraffic="true"> in your manifest (documentation). But for the security of your users, please consider this a last resort.

Answered by Thomas on January 13, 2021

Remove following...

mywebview.setBackgroundResource(R.drawable.wordpress);
mywebview.setBackgroundColor(Color.TRANSPARENT);

from XMl:

android:background="@android:color/transparent"

Answered by chand mohd on January 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP