Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

URL not loading in WebView

Hi Ben Jakuben

I'm trying to load a URL in an android WebView, which is in a Fragment. I was able to accomplish this, but the website was launching the default browser. I found the setWebViewClient method and it kept the website within the WebView inside of the app versus launching the default browser. However - this was showing a blank page. I tried other websites (http://www.google.com, http://www.amazon.com, etc) and these work...however the site that I am trying to get to load (http://www.cleankutz.appointy.com) does not. Neither does http://www.walmart.com.

I've set javascript enabled just in case - but cannot understand why some URLs would load and others wont from within the app's WebView. Would you have any ideas?

Thanks so much!

Ah! Its working now. I had to include an "s" at the end of "http". Im not sure why exactly, but it works now. I need to learn more about URLs apparently. The page loads on my desktop and on my device's Chrome browser without the "s". Why is Androids webview inside the app more particular than the Google Chrome browser on the same phone?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi Aaron Watkins, glad you got this working! Sorry for my late reply!

Would you mind pasting in your code? It's hard to say why that might be happening. I'm glad making it https is working, but you shouldn't have to do that! :smile:

1 Answer

sure thing...here it is. google was loading fine as well as a few other sites...but certain sites required the "s" it seemed...not exactly sure why.

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;
import android.webkit.WebSettings;


public class ScheduleActivity extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View mainView = (View) inflater.inflate(R.layout.schedule_layout, container, false);
        WebView webView = (WebView) mainView.findViewById(R.id.webView);

        webView.getSettings().setJavaScriptEnabled(true);



        webView.setWebViewClient(new webViewClient());
        //webView.loadUrl("http://www.cleankutz.appointy.com");
        webView.loadUrl("https://cleankutz.appointy.com/");

        return mainView;


        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.schedule_layout, container, false);

    }



}