'Android WebView Not showing Anything
I am trying to use webView to show a URL in android app.
If I try with this URL-
But if I am trying with this-
I am getting this-
What I have done is-
Activity-
package com.appnucleus.abrarjahin.hello8920;
import android.content.Context;
import android.net.ConnectivityManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;
public class ActivityMain extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( isNetworkAvailable() ) //check if internet available or not
{
Toast.makeText(
ActivityMain.this,
"Internet Connected",
Toast.LENGTH_LONG
).show();
WebView webview = (WebView)findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(getString(R.string.sound_cloud_url));
}
else //Not connected
{
Toast.makeText(
ActivityMain.this,
"Internet Disconnected",
Toast.LENGTH_LONG
).show();
}
}
//<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//<uses-permission android:name="android.permission.INTERNET"/>
public boolean isNetworkAvailable()
{
final ConnectivityManager connectivityManager = ((ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
}
Layout-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.appnucleus.abrarjahin.hello8920.ActivityMain">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
strings.xml-
<resources>
<string name="app_name" translatable="false">Hello 8920</string>
<string name="sound_cloud_url">https://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F8111706%3Fsecret_token%3Ds-3aK27&color=e2ef3a&auto_play=false&show_artwork=false</string>
<string name="test_url">http://stackoverflow.com/</string>
</resources>
AndroidManifest.xml-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appnucleus.abrarjahin.hello8920">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".ActivityMain">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Can anyone please help why I am getting a blank page?
Solution 1:[1]
Try this Code:-
public class ActivityMain extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if ( isNetworkAvailable() ) //check if internet available or not
{
Toast.makeText(
ActivityMain.this,
"Internet Connected",
Toast.LENGTH_LONG
).show();
WebView webview = (WebView)findViewById(R.id.webView);
webView.setWebViewClient(new myWebClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl(getString(R.string.sound_cloud_url));
}
else //Not connected
{
Toast.makeText(
ActivityMain.this,
"Internet Disconnected",
Toast.LENGTH_LONG
).show();
}
}
//<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
//<uses-permission android:name="android.permission.INTERNET"/>
public boolean isNetworkAvailable()
{
final ConnectivityManager connectivityManager = ((ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE));
return connectivityManager.getActiveNetworkInfo() != null && connectivityManager.getActiveNetworkInfo().isConnected();
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}
}
Solution 2:[2]
Try this,
webView = (WebView) findViewById(R.id.webView);
**webView.setWebViewClient(new WebViewClient());**
String url = "https://www.google.co.in";
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
This may helps you.
Solution 3:[3]
Add WebViewClient
on WebView and check what error you are getting.
How to set it ?
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);
Log.e("Error", description);
}
});
Hope this will make sense.
Solution 4:[4]
You can override the onReceivedSslError
in you webViewClient
.
aboutALCWebView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
Log.d("Ssl Error:",handler.toString() + "error:" + error);
handler.proceed();
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Payal Patel |
Solution 2 | Sathish Kumar J |
Solution 3 | Hiren Patel |
Solution 4 | ivcubr |