2

I'm trying to make a webview application but I'm not good about it.

Just I wanna do that: I have a website and I wanna open external links in default browser like a target _blank.

My Activity:

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
NavigationView navigation;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    OneSignal.startInit(this).init();
    initInstances();
    WebView myWebView = (WebView) findViewById(R.id.webView1);
    myWebView.loadUrl("http://mywebsite.domain.com/myprofile/");
    myWebView.setWebViewClient(new WebViewClient());
}

private void initInstances() {
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    drawerToggle = new ActionBarDrawerToggle(MainActivity.this, drawerLayout, R.string.hello_world, R.string.hello_world);
    drawerLayout.setDrawerListener(drawerToggle);

    navigation = (NavigationView) findViewById(R.id.navigation_view);
    navigation.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

    String menu1 = "https://mywebsite.domain.com/myprofile/";
    String menu2 = "https://mywebsite.domain.com/exit";
    String menu3 = "https://mywebsite.domain.com/discover/";
    String menu4 = "https://mywebsite.domain.com/settings/";
    String menu5 = "https://mywebsite.domain.com/language";
    String menu6 = "https://mywebsite.domain.com/vipmembership";

        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            int id = menuItem.getItemId();
            WebView myWebView = (WebView) findViewById(R.id.webView1);
            myWebView.setWebViewClient(new WebViewClient());
            switch (id) {
                case R.id.navigation_item_1:
                    myWebView.loadUrl(this.menu1);
                    break;
                case R.id.navigation_item_2:
                    myWebView.loadUrl(this.menu2);
                    break;
                case R.id.navigation_item_3:
                    myWebView.loadUrl(this.menu3);
                    break;
                case R.id.navigation_item_4:
                    myWebView.loadUrl(this.menu4);
                    break;
                case R.id.navigation_item_5:
                    myWebView.loadUrl(this.menu5);
                    break;
                case R.id.navigation_item_6:
                    myWebView.loadUrl(this.menu6);
                    break;
            }
            return false;
        }
    });


}

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item))
        return true;

    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}}

I tired so many code about it but it's didn't work. Thanks a lot.

2

1 Answer 1

5

You have to send an Intent to to the default web browser, in order to open the link in the browser, so maybe something like this:

Create a class that extends WebViewClient, in this way:

private class MyCustomWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.contains("https://mywebsite.domain.com")) {
            Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(i); 
        } else {
            view.loadUrl(url);
        }
        return true;
    }
}

In your MainActivity, in the onCreate method, set the MyCustomWebViewClient before the call to loadUrl method, like this:

public class MainActivity extends AppCompatActivity {

    DrawerLayout drawerLayout;
    ActionBarDrawerToggle drawerToggle;
    NavigationView navigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OneSignal.startInit(this).init();
        initInstances();
       WebView myWebView = (WebView) findViewById(R.id.webView1);
       myWebView.setWebViewClient(new MyCustomWebViewClient());
       myWebView.loadUrl("http://mywebsite.domain.com/myprofile/");
    }

    // the rest of your code
Sign up to request clarification or add additional context in comments.

4 Comments

I'll try this. Thanks.
You're welcome. Don't forget to accept the best answer, please
Thanks a lot when I modified your code, it's worked! :)
Note that shouldOverrideUrlLoading(WebView, String) is deprecated since sdk v24 - you should instead implement the shouldOverrideUrlLoading(WebView, WebResourceRequest) if you target later devices or both.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.