2

I have a webview set up but when I click on the item, an empty fragment shows up where the actual web item is supposed to be. I have tried loading my urls on mobile browser and they work fine. I have an internet permission in android manifest.

web view layout file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.HomeActivity">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

webview fragment

public class WebViewDetailsFragment extends Fragment {

    private WebView detailsWebView;
    private String articleSource;

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

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        detailsWebView = view.findViewById(R.id.webView);
        detailsWebView.getSettings().setLoadsImagesAutomatically(true);
        detailsWebView.getSettings().setJavaScriptEnabled(true);
        detailsWebView.getSettings().setUseWideViewPort(true);
        detailsWebView.getSettings().setLoadWithOverviewMode(true);
        detailsWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        // Configure the client to use when opening URLs
        detailsWebView.setWebViewClient(new WebViewClient());
        setItems();
        detailsWebView.loadUrl(articleSource);


    }

    private void setItems(){
        NewsArticle newsArticle = NewsFragment.news.get(NewsAdapter.pos);
        articleSource = "https://www.google.com";
    }
}
personX
  • 39
  • 6
  • I just tried to run your code and it works fine. The only problem that I know was about HTTP/HTTPS but in my case both worked properly. Maybe you would be able to get solution here: [link](https://stackoverflow.com/a/26977516/9871059) – Blind Kai Aug 09 '19 at 21:25
  • Did www.google.com actually show up or did an empty screen show? – personX Aug 09 '19 at 21:28
  • for me it works even without URL changes. I had problems with SSL certificate previously in other page. – Blind Kai Aug 09 '19 at 21:29
  • which ssl certificate? what does that mean? how can i fix it in my code? – personX Aug 09 '19 at 21:40
  • You could check the link in previous comment. It solves most number of problems with that. Otherwise I don't know how to help. – Blind Kai Aug 09 '19 at 21:46
  • I can see the url when I run it in terminal but the url is not actually showing up on an android screen. Just a blank screen. – personX Aug 09 '19 at 21:46
  • I fixed it. I had to have my webview in relative layout. Thanks though. – personX Aug 09 '19 at 22:16

1 Answers1

0

I too tried your Code. it is working fine and good.There may be a chances of issue in fragmentManager. Did you checked the view is visible or not

Rama
  • 79
  • 8