webview inside Fragnent I want to open my site inside the webview, and the links inside the site should open outside

Asked

Viewed 251 times

-1

REINFORCEMENT I AM OPENING WEBVIEW IN A FRAGMENT AND NOT IN MAIN ACTIVITY - I want to open my site inside the webview, and the links from other sites should open outside, in the normal mobile browser, but this does not occur. My webview is inside a Fragment. You can help me?

MY CODE :

@Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_principal, container, false);
    WebView webView = (WebView) v.findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);

    WebViewClientImpl webViewClient = new WebViewClientImpl(this);

    //se eu desabilito essa linha resolve, porem tudo abre fora do app, e 
    //queria somente que os links listados no site abrissem fora.

    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("https://meusite.com/");

    return v;

}

    public class WebViewClientImpl extends WebViewClient {


        private PrincipalFragment activity = null;
        public WebViewClientImpl(PrincipalFragment activity) {
            this.activity = activity;
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView webView, String url) {
            if(url.contains("https://meusite.com")) return false;
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            activity.startActivity(intent);

            return true;

        }

    }

Fragmente_principal.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@android:color/holo_purple">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </WebView>
</LinearLayout>
  • I tried and does not solve, I am opening the webview inside a Fragment! If you have any interesting suggestions!

  • Any mistakes? Are you sure you put the classes in the right places? You put a Log. i() inside shouldOverrideUrlLoading to make sure that the method is called? Can you do a simple debug with Log. i, Log. d, etc? Do you know how to use break-point? Can you give more details? If you can notice the problem edit the question and be very clear where it failed.

  • the problem is that there are no errors, I just wanted my domain.com.br to stay inside the webview, and the links inside the site open outside the webview, when I do it outside of Facebook works... I’m doubtful where to put the method , because as said startActivity(i); gives error solutions are : add Qualifier Activity to method Creat Method Startactivity and ...

  • I have a public class Webviewclientimpl extends Webviewclient , it is within it that I will put the solution that informed me correct? the method I have already created in this class and in the Principalfragment class... but without success

  • public class Webviewclientimpl extends Webviewclient { private Principalfragment Activity = null; public Webviewclientimpl(Principalfragment Activity) { this.Activity = Activity; } @Override public Boolean shouldOverrideUrlLoading(Webview webView, String url) { if(url.contains("my.com")) Return false; Intent Intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); Activity.startActivity(Intent); Return true; } }

  • Try to intercept the links and treat them to open with a browser

  • Principalfragment does not let use @ public View onCreateView(Layoutinflater Inflater, aNullable ViewGroup container, a*Nullable Bundle savedInstanceState) {&#xA;&#xA; View v = inflater.inflate(R.layout.fragment_principal, container, false);&#xA; WebView webView = (WebView) v.findViewById(R.id.webview);&#xA; webView.getSettings(). setJavaScriptEnabled(true);&#xA;&#xA; WebViewClientImpl webViewClient = new WebViewClientImpl(this);&#xA;&#xA; &#xA; //webView.setWebViewClient(new WebViewClient());&#xA; webView.loadUrl("https://meu.com/");&#xA;&#xA; return v;

  • @Murillocomino how do I do it buddy? I never thought about it!

  • Put the code in the question and not in the comments, post everything (removing data as passwords and addresses of Pis) because only with this can not be sure.

  • Okay, I’ll do it

  • I updated look if help, put my original code, I’ve tried several methods here stack, but either open everything inside my Fragment or open everything outside ... I want to thank everyone for the attention so far!

  • @Wouldn’t Guilhermenascimento have an if on Principalfragmente that checks if the URL is the same as on my site and if it isn’t open outside?? I just couldn’t structure it here...

  • @Júnioralencar do not know how this your code actually, you can have some 20 Fragments, subfragments, intents, etc. It can be anything there that failed, I use basically the same code, company links open in the webview, links from other sites open in the main browser of the user. It is likely that you are making some confusion. It came to put a Log inside shouldOverrideUrlLoading to see if it’s working?

  • I don’t know how to put Log buddy, can you explain?

  • Well I just wanted a help, my code is so and as I explained it occurs this, if you prefer I can send you the code or the app if you want to see. Thanks in advance for the readiness to help! I apologize for not being so clear...

  • Just open out but then open all links when I comment on this line = webView.setWebViewClient(new Webviewclient()); Without comment everything opens inside the app. When I do in a normal Activity other than Fragmente, it works normal, but in this way disables the nav_s of my menu which is something I cannot change.

  • Junior your code is simply different from what I suggested at the beginning, do exactly like this: https://answall.com/a/143300/3635 and exchange if(uri.getHost().equals("exemplo.com"){ and trade for if(uri.getHost().equals("meusite.com"){, is just the site address, no file, folders or bars, just something like www.meusite.com, with nothing else.

  • @Guilherme, I’ve tried this, the problem is that is giving err on this isOnline, on getApplicationContext, startActivity, setContentView, and on conexaofail , and all imports or changes that android studio suggests does not solve, because it creates methods that do not make the app run and yes generates other errors like the isOnline it generates a method or a getter that generate another error... and any treatment that having do the app nor open...

  • If you can explain to me what these variables are for, because in my app they appear as unused and that’s why the error occurs...

Show 14 more comments

1 answer

1


Following the way you implemented webview instead of:

webView.setWebViewClient(new WebViewClient());

the right is to instantiate the class you created that extends Webviewclient, then it would be:

webView.setWebViewClient(webViewClient);

The rest of your Fragment keeps.

In the Webviewclientimpl class let the override method look like this:

@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
    if(url.contains("https://meusite.com")) {
        return true;
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
        //Use o método a seguir caso, o seu webview continua carregando
        //as páginas que o browser deveria abrir (diferente do seu host) 
        webView.stopLoading();

        return false;
    }

}

Or you can override this method the way Guilherme Nascimento suggested:

@Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {

     Uri uri = Uri.parse(url);

    if(uri.getHost().equals("meusite.com")) {
        return true;
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(intent);
        //Use o método a seguir caso, o seu webview continua carregando
        //as páginas que o browser deveria abrir (diferente do seu host) 
        webView.stopLoading();

        return false;
    }

}

I hope I helped. And remember to change the urls.

  • Says @Murilo, Our brother now yes, this opening the link that should be outside is opening in the browser, only detail is that the links of my site now do not load either inside the webview or outside, I tested with your code and with Guilherme’s and I put your code without changing the webView.setWebViewClient(new Webviewclient()); then back to Bir everything outside... our must be angry with me, the strange is that if one thing works the other not... but webView.stopLoading(); I didn’t even imagine it, only detail now is that the links on my site do not open, the others are opening out perfectly .

  • I was able to solve friend, I did the following: I used your code that saved my life, and I added the following line : webView.loadUrl(url); This was the code: public Boolean shouldOverrideUrlLoading(Webview webView, String url) { if(url.contains("meusite.com/")) { webView.loadUrl(url); Return true; The rest of the code matches the one you gave me! Thank you all from my heart for your help! How can I thank you all?

  • I edit here in answer the question? never used this social network, but I am so happy that I need to somehow thank you!!!

  • @Juniors think it’s good that you solved it, but if you’re already calling the url out, you wouldn’t need to call in again. To mark my answer just go to her top on the left and mark as answered and/or as useful.

  • 1

    Well eh, but it was way it worked 100%. Maybe it’s version of my Android, 3.0.1. Anyway I am grateful for more!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.