0

Im building an App for my school these days and one drawer fragment contains a webview with school-news. There are buttons under the articles which open a new link with full articles after clicking them. (like "read more" - Buttons).

I want to open a new activity after cliking them, independently of which article is clicked, s that i dont need to create an activity for every single news-article.

I hope you can understand my problem and give me a solution. Thanks

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:background="@android:color/white">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>





<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipe"
    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"
        android:visibility="gone"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</RelativeLayout>






    static WebView webview;
    private View mContentView;
    SwipeRefreshLayout swipe;





    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case 1:{
                    webViewGoBack();
                }break;
            }
        }
    };


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View v = inflater.inflate(R.layout.fragment_news, container, false);





        swipe = (SwipeRefreshLayout) v.findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                loadWeb(v);

            }
        });

        loadWeb(v);
        return v;



    }






    private void webViewGoBack(){
        webview.goBack();
    }





    public void loadWeb(final View v) {
        webview = (WebView) v.findViewById(R.id.webView);
        WebSettings webSettings = webview.getSettings();
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webview.getSettings().setAppCacheEnabled(true);
        webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY );
        webSettings.setDomStorageEnabled(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);
        webSettings.setSavePassword(true);
        webSettings.setSaveFormData(true);
        webSettings.setEnableSmoothTransition(true);


        webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webview.setLayerType(WebView.LAYER_TYPE_NONE, null);
        swipe.setRefreshing(true);
        webview.setWebViewClient(new WebViewClient() {


            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                swipe.setRefreshing(false);


                {

                    webview.loadUrl
                            ("javascript:(function() { " +
                                    "document.getElementsByTagName('header')[0].style.display='none'; " +
                                    "document.getElementsByTagName('nav')[0].style.display='none'; " +
                                    "document.getElementsByTagName('footer')[0].style.display='none'; " +
                                    "document.getElementsByClassName('widgettitle')[0].remove(); " +
                                    "document.getElementsByClassName('widgettitle')[0].remove(); " +
                                    "document.getElementsByClassName('widgettitle')[0].remove(); " +
                                    "document.getElementsByClassName('widget widget_search')[0].remove(); " +
                                    "document.getElementsByClassName('widget widget_recent_entries')[0].remove(); " +
                                    "document.getElementsByClassName('widget widget_archive')[0].remove(); " +
                                    "document.getElementsByClassName('cat-item cat-item-3')[0].remove(); " +
                                    "document.getElementsByClassName('l-header pos_fixed bg_solid shadow_wide')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_taxonomy_1 color_link_inherit  post_taxonomy style_simple')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('w-grid-item-elm usg_post_author_1 color_link_inherit  post_author vcard author')[0].remove(); " +
                                    "document.getElementsByClassName('grecaptcha-badge')[0].remove(); " +
                                    "document.getElementsByClassName('rc-anchor-logo-img rc-anchor-logo-img-large')[0].remove(); " +


                                    "})()");

                    webview.setVisibility(View.VISIBLE);


                }
            }








        });

        webview.loadUrl("https://www.bbs-varel.de/aktuelles/");
        webview.setOnKeyListener(new View.OnKeyListener(){




            public boolean onKey(View v, int keyCode, KeyEvent event) {




                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getAction() == MotionEvent.ACTION_UP
                        && webview.canGoBack()) {
                    handler.sendEmptyMessage(1);
                    return true;
                }

                return false;
            }

        });





    }



    }

1 Answers1

0

There's a way to do is. In short, it consist of registering a listener with webview, so that each page navigation (ie new page loaded when button is clicked) is passed on to you first.

To do this:

Webview webview = findViewById(R.id.webView)
webView.setWebViewClient(webViewClient); 
webView.setWebChromeClient(new WebChromeClient());

Where webViewClient is your class implementing android.webkit.WebViewClient. There, you'll find a method android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String) which gives you a 'hook' into checking whether user clicked something interesting for you, or not. If you return true, new page won't be loaded. If you return false, the page will behave like it would without listener attached. Good luck!

dominik4142
  • 556
  • 3
  • 14