Aliaksandr Makarevich
Aliaksandr Makarevich

Reputation: 11

Fragment in Android: using WebView with external links

I create app and have problem with using WebView with external links in Fragment.

I have webView_layout.xml

<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"
    android:orientation="vertical"
    tools:context="by.beep.materialvebweiv.activity.FriendsFragment">

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView" />

</RelativeLayout>

And class

package by.beep.materialvebweiv.activity;

import android.app.Activity;
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;

import by.beep.materialvebweiv.R;

public class FriendsFragment extends Fragment {

    public FriendsFragment() {
    }

    public WebView mWebView;

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_friends, container, false);
        mWebView = (WebView) v.findViewById(R.id.webView);
        mWebView.loadUrl("https://google.com");

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.setWebViewClient(new WebViewClient());

        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

I try to use WebView in Fragment with next case: if URL don't contains "google.com" redirects to open link in default browser.

Can anybody help me?

Upvotes: 1

Views: 198

Answers (1)

Valve Social
Valve Social

Reputation: 11

Try moving public WebView mWebView; above public FriendsFragment(){}

That will give you the following code:

public class FriendsFragment extends Fragment {

    public WebView mWebView;
    public ProgressBar progressBar;
    public FrameLayout frameLayout;
    public SwipeRefreshLayout swipeRefreshLayout;

    public FriendsFragment() {
    }


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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.fragment_friends, container, false);
        mWebView = (WebView) v.findViewById(R.id.webView);
        mWebView.loadUrl("https://google.com");

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.setWebViewClient(new WebViewClient());

        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}

Upvotes: 1

Related Questions