Open an Url within an Activity

2

I have the following question: How to Open an Url in an Activity.

This activity is my Activity_ Main, in it I have an image of a cardview with Url, but when I click it opens the browser options, but what I want is for these links to be opened within my application.

"I've searched for tutorials but everyone talks about WebView" the truth is that I do not want a web view inside my activity_main. I have an AcivityWeb that works well for other uses like going to my website from a FAB, but it's not the Main.

Important! These links come from the firebase.

    
asked by Elihat Caceres 04.01.2017 в 20:18
source

2 answers

2

Use WebViewClient , and within the method shouldOverrideUrlLoading() loads in the same view the url, this way when clicking on a link will be forced to load within the WebView . :

WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://stackoverflow.com");
webView.setWebViewClient((new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            //Al dar clic en un link se obligará a cargar dentro del WebView.
            view.loadUrl(url);
            return true;
        }
    }));

We load the page within WebView .

By clicking on a link, it will open within the same WebView .

    
answered by 04.01.2017 в 21:06
0

You must use the WebViewClient to not leave the application. I'm not sure this is what you need.

    WebView appWeb = (WebView) this.findViewById(R.id.webView);
    //Habilitamos el javaScript y el zoom
    appWeb.getSettings().setJavaScriptEnabled(true);
    appWeb.getSettings().setBuiltInZoomControls(true);
    //Cargamos el enlace definido
    appWeb.loadUrl(direccion_web);
    //Este método es para que el navegador se quede en nuestra aplicación
    appWeb.setWebViewClient(new WebViewClient(){
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
       return false;
    }
    
answered by 04.01.2017 в 20:55