Skip to content Skip to sidebar Skip to footer

Trying To Add A Second Link To App Menu Android

i'm trying to add a second link to the menu. the first one has worked fine. the second is called menu_about package com.test.apppackage; import android.app.Activity; import androi

Solution 1:

You have 2 onOptionsItemSelected:

publicbooleanonOptionsItemSelected(MenuItem item) { // <-- this
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl("https://www.google.com/news");
                setContentView(mWebview );
                break;

        }
        returnsuper.onOptionsItemSelected(item);
    }



    publicclassMyWebViewClientextendsWebViewClient {

        @OverridepublicbooleanshouldOverrideUrlLoading(WebView view, String url) {
               view.loadUrl(url);
               returntrue;
           }
   }


    publicbooleanonOptionsItemSelected(MenuItem item) { // <-- and this
        int id = item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl("https://www.bbc.co.uk");
                setContentView(mWebview );
                break;

        }
        returnsuper.onOptionsItemSelected(item);
    }

Delete one of them and switch the id like this:

publicbooleanonOptionsItemSelected(MenuItem item) {
        intid= item.getItemId();
        switch(id) {
            case com.homeovitality.productrecommender.R.id.menu_reset:
                mWebview .loadUrl("https://www.google.com/news");
                setContentView(mWebview );
                break;
            case com.homeovitality.productrecommender.R.id.menu_about:
                mWebview .loadUrl("https://www.bbc.co.uk");
                setContentView(mWebview );
                break;


        }
        returnsuper.onOptionsItemSelected(item);
    }

Post a Comment for "Trying To Add A Second Link To App Menu Android"