Placing findViewById on the fragment

Asked

Viewed 146 times

1

I am trying to put a webView inside a fragment that refers to webView that I put inside the xml for the findViewByIdmethod Fragment only works if I extend an Activity class. Is there any way I can use it on Fragment as well?

public class VideosFragment extends Fragment


{

private WebView webView;

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


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

    webView = (WebView) findViewById(R.id.xp4);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("https://www.https://www.google.com/");
}

  }

findViewById has an error indicating that the method is undefined.

1 answer

1


To call the webview within your fragment, before the webview has to be in the same fragment xml file. Inside the method onCreate do:

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

    webView = view.findViewById(R.id.xp4);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("https://www.google.com/");

    return view;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.