John Spring
John Spring

Reputation: 49

Webview does not show up at all

Here are my codes and I cannot see any webview on my application. I tried to create a button and I could see that. When I ran my app, there is only a white blank space in place of webview.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top"
android:orientation="vertical"
tools:context="com.example.user.calisma1.MainActivity">

<Button
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:id="@+id/b"/>

<WebView
    android:id="@+id/wv"
    android:layout_width="match_parent"
    android:layout_height="300dp"></WebView>

import java.util.Random;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {


WebView wv2;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    wv2 = findViewById(R.id.wv);
    wv2.getSettings().setJavaScriptEnabled(true); 
    wv2.loadUrl("https://webmail.etu.edu.tr/");
    b = findViewById(R.id.b);
}


}

Upvotes: 0

Views: 112

Answers (2)

borosuman
borosuman

Reputation: 335

Your url(https://webmail.etu.edu.tr/) involves redirection with HTTPS(SSL) so, following lines should be added.

  wv2.setWebViewClient(new WebViewClient() {
        @Override
        public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }
        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            // Handle the error
        } 

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }
    });
wv2.loadUrl("https://webmail.etu.edu.tr/");

Upvotes: 0

Naimish Vinchhi
Naimish Vinchhi

Reputation: 803

Add internet permission in manifest, if not added:

<uses-permission android:name="android.permission.INTERNET"/>

Add below code before loadUrl() to set webview client:

webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });

Upvotes: 1

Related Questions