androidwebview

Android WebView Not showing Anything


I am trying to use webView to show a URL in android app.

If I try with this URL-

http://stackoverflow.com

I am getting this- enter image description here

But if I am trying with this-

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

I am getting this-

enter image description here

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&amp;color=e2ef3a&amp;auto_play=false&amp;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

  • 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.