-1

I have encountered a problem which i'm unable to solve, I googled a lot but couldn't find a solution.

I made an app using master detail/flow. This app opens a webpage whenever a category item is selected but when i click another link in the webpage it redirects to browser and opens the link in the browser. Is there a way to open all the links in the app itself? All help is much appreciated! I'm all out of ideas here. Thank you.

WebpageDetailFragment.java

public class WebpageDetailFragment extends Fragment {

    public static final String ARG_ITEM_ID = "item_id";

    private DummyContent.DummyItem mItem;

    public WebpageDetailFragment() {
    }

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

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));

            Activity activity = this.getActivity();
            CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout);
            if (appBarLayout != null) {
                appBarLayout.setTitle(mItem.item_name);
            }
        }
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.webpage_detail, container, false);

        // Show the dummy content as text in a TextView.
        if (mItem != null) {
            ((WebView) rootView.findViewById(R.id.detail_area)).loadUrl(mItem.url);
        }
        return rootView;
    }
}

I have another Problem when I add your code the same page is load in the WebView the problem of redirecting to the browser is solved but it's not loading the required page. Here my code:-

DummyContent.java

public class DummyContent {

    public static final List<DummyItem> ITEMS = new ArrayList<>();

    public static final Map<String, DummyItem> ITEM_MAP = new HashMap<>();

    static {
       addItem(new DummyItem("1", "Home", "http://techonaclick.com/"));
        addItem(new DummyItem("2", "About Us", "http://techonaclick.com/about-2/"));
        addItem(new DummyItem("3", "Contact Us", "http://techonaclick.com/contact-us/"));
        addItem(new DummyItem("4", "Disclaimer", "http://techonaclick.com/disclaimer/"));
        addItem(new DummyItem("5", "Privacy Policy", "http://techonaclick.com/privacy-policy/"));
    }

    private static void addItem(DummyItem item) {
        ITEMS.add(item);
        ITEM_MAP.put(item.id, item);
    }

    public static class DummyItem {
        public final String id;
        public final String item_name;
        public final String url;

        public DummyItem(String id, String item_name, String url) {
            this.id = id;
            this.item_name = item_name;
            this.url = url;
        }

        @Override
        public String toString() {
            return item_name;
        }
    }
}

The place where i inserted your code

Evin1_
  • 10,198
  • 8
  • 38
  • 47

1 Answers1

0

Add a new WebViewClient() to your main WebView to handle all the links.

From the documentation:

When the user clicks a link from a web page in your WebView, the default behavior is for Android to launch an application that handles URLs. Usually, the default web browser opens and loads the destination URL. However, you can override this behavior for your WebView, so links open within your WebView. You can then allow the user to navigate backward and forward through their web page history that's maintained by your WebView.


Example:

myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});

In your case:

Add this snippet in any place within your Fragment class:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    WebView webView = (WebView) view.getRootView().findViewById(R.id.detail_area);
    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
}
Evin1_
  • 10,198
  • 8
  • 38
  • 47