4

I am trying to create a WebView in a Fragment which should be loaded. I added it already to my xml File. However, the findViewById method only works if I extend an Activity class. Is there anyway of which I can use it in Fragment as well?

I already tested the Solution from another Question, but it doesn't work.

public class OneFragment extends Fragment{

public WebView PlanWebView;

public OneFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    PlanWebView WebView = (PlanWebView) getView().findViewById(R.id.PlanWebView);
    // Unknown class: "PlanWebView";
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_one, container, false);
}

}
Quick learner
  • 10,632
  • 4
  • 45
  • 55

3 Answers3

3

Try this please , This is the OneFragment class , Webview needs a view which has to be inflated using xml

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout. fragment_one, container, false);
    mWebView = (WebView) view.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 view;
}

and xml for fragment_one

<?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"
    >

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>
Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • Thanks for answering! I have another problem... How i can do a reload this WebView in a Fragment? Without Fragments and in a single MainActivity it is very easy... I added a menu in the MainActivity, but where must i add the public boolean for the menu? If i add this in the One_Fragment.class i get a crash after pressing the update button in the menu. –  Nov 16 '17 at 17:11
  • `@Override public boolean onOptionsItemSelected(MenuItem menuitem) { int id = menuitem.getItemId(); if (id == R.id.update) { OneFragment.this.PlanWebView.loadUrl("http://google.de"); } return false; }` –  Nov 16 '17 at 17:15
  • Hello dear ,can u please explain about your new issue i am not able to get it? Do you mean you want to reload webview from the main activity? – Quick learner Nov 16 '17 at 17:34
  • Yes exactly. Reload from MainActivity or from One_Fragment Activity. –  Nov 16 '17 at 17:35
  • okay you should use interface to do that , https://stackoverflow.com/questions/21228721/how-to-replace-a-fragment-on-button-click-of-that-fragment – Quick learner Nov 17 '17 at 04:28
  • look at this and try it. – Quick learner Nov 17 '17 at 04:28
  • Thanks, but this solution opens the same fragment over the fragment. However, only the URL in the WebView should be reloaded by pressing the update button in the menu. –  Nov 17 '17 at 13:32
0

in Fragment you can declare your variables in onCreateView method.

public class OneFragment extends Fragment{

public WebView PlanWebView;

public OneFragment() {
// Required empty public constructor
}

@Override
 public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    }

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {


 }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_one, container, false);
PlanWebView  = (WebView)view.findViewById(R.id.PlanWebView);
return view;
   }

 }
0

You should use code below in onCreateView method:

View view = inflater.inflate(layout,container,false);

WebView webview = (WebView) view.findViewById(R.id.webView);

return view;
Vinay Prajapati
  • 7,199
  • 9
  • 45
  • 86
Naman
  • 1