18

recently I started creating an Android app with WebView. The app has grown and I changed the activity with WebView to tabbed activity using fragments and ViewPager. And there started my issue. After some headaches I finally managed how to display proper fragments for each tab. Then I simply copied the code for WebView to one of the fragment but the fragment isn't showing the web page I want. Any help will be appreciated!

Here's the code:

fragment with webView

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.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class WebViewFragment extends Fragment { public WebView mWebView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mWebView = (WebView) mWebView.findViewById(R.id.webview); mWebView.loadUrl("google.com"); // Enable Javascript WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); // Force links and redirects to open in the WebView instead of in a browser mWebView.setWebViewClient(new WebViewClient()); return inflater.inflate(R.layout.fragment_bugtracker, container, false); } } 

and xml

<?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" tools:context=".Tabs$BugtrackerFragment"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> 
1
  • 10
    @Duggu how is this a duplicate? Commented Jul 1, 2015 at 11:03

1 Answer 1

52

Things to do like

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v=inflater.inflate(R.layout.fragment_bugtracker, container, false); mWebView = (WebView) v.findViewById(R.id.webview); mWebView.loadUrl("https://google.com"); // Enable Javascript WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); // Force links and redirects to open in the WebView instead of in a browser mWebView.setWebViewClient(new WebViewClient()); return v; } 
Sign up to request clarification or add additional context in comments.

2 Comments

Nice, but I don't understand one thing, why is defining and returning v something else than just returning the definition of it?
'v' denote here the View which is inflate. that is user defined variable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.