2

I am having cart icon on ToolBar.When I try to click,it didn't show any action for a long time.After keep on clicking it, Toast message that I set is enabled or the app is crashed.

Please help me to fix it.

enter image description here

My Code is:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
        mCounter = (TextView) badgeLayout.findViewById(R.id.counter);
        badgeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "Cart is Working", Toast.LENGTH_SHORT).show();
                Intent next = new Intent(context, ProductActivity.class);
                startActivity(next);
            }
        });
return true;
}

UPDATE:

     public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SearchView.OnCloseListener, SearchView.OnQueryTextListener {
            public static final String MyPREFERENCES = "Preference";
            public static final String SEARCHING_DATA = "DATA_SEARCH";
            public static String strTabName;
            public static TextView mCounter;
            public static RelativeLayout badgeLayout;

            public ArrayList<String> values;
            Toolbar toolbar;
            Context context;
            Config config;
            ArrayList<String> tabName = new ArrayList<String>();
            SharedPreferences pref;
            TextView txtDash_Title, txtDash_Des;
            String passQuery;
            FragmentManager mFragmentManager;
            FragmentTransaction mFragmentTransaction;
            String possibleEmail;
            String GOOGLE_USERNAME;
            private int count = 0;
            private GoogleApiClient client;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                context = this;

                pref = context.getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
                setupToolbar();
                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
                drawer.setDrawerListener(toggle);
                toggle.syncState();

                NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
                navigationView.setNavigationItemSelectedListener(this);

                txtDash_Title = (TextView) findViewById(R.id.dashboard_txt_title);
                txtDash_Des = (TextView) findViewById(R.id.dashboard_txt_des);

                TextView myTextview = (TextView) findViewById(R.id.textView);
                if (haveNetworkConnection())
                    new TabNameSync().execute();
                else
                    Toast.makeText(context, "No Network Connection", Toast.LENGTH_SHORT).show();
                setupCollapsingToolbar();
                getGOOGLEUSERNAME();

            }

            private void getGOOGLEUSERNAME() {
                Cursor c = getApplication().getContentResolver().query(ContactsContract.Profile.CONTENT_URI, null, null, null, null);
                c.moveToFirst();
                GOOGLE_USERNAME = c.getString(c.getColumnIndex("display_name"));
                Log.e("Google Username", GOOGLE_USERNAME);
                c.close();
            }

            private void setupCollapsingToolbar() {
                final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(
                        R.id.collapse_toolbar);

                collapsingToolbar.setTitleEnabled(false);
            }

            private void setupToolbar() {
                toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);
                getSupportActionBar().setTitle("Welcome to Scoop Shop!");
            }

            @SuppressWarnings("StatementWithEmptyBody")
            @Override
            public boolean onNavigationItemSelected(MenuItem item) {
                int id = item.getItemId();

                if (id == R.id.nav_camera) {
                } else if (id == R.id.nav_gallery) {

                } else if (id == R.id.nav_slideshow) {

                } else if (id == R.id.nav_manage) {

                } else if (id == R.id.nav_share) {

                } else if (id == R.id.nav_send) {

                }

                DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.main, menu);
                badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
                mCounter = (TextView) badgeLayout.findViewById(R.id.counter);
                badgeLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context, "Cart Working", Toast.LENGTH_SHORT).show();
                        Intent next = new Intent(context, ProductActivity.class);
                        startActivity(next);
                    }
                });
        //        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        //        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        //        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        //        searchView.setSubmitButtonEnabled(false);
        ////        searchView.getOutlineProvider();
        //        searchView.setOnQueryTextListener(this);
        //        searchView.setOnCloseListener(this);
                //       return super.onCreateOptionsMenu(menu);
                return true;
            }

            @Override
            public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_search:
                        break;
                    case R.id.badge:
                        Toast.makeText(context, "Cart is Working", Toast.LENGTH_SHORT).show();
               /*         Intent next = new Intent(context, ProductActivity.class);
                        startActivity(next);*/
                        return true;
                    default:
                        return super.onOptionsItemSelected(item);
                }
                return super.onOptionsItemSelected(item);
            }
    }
Parama Sudha
  • 2,583
  • 3
  • 29
  • 48

3 Answers3

1

The issue may already be closed, but I found possible cause:

  • if you used Relative Layout in your .xml file, neighboring elements can cover your toolbar. So, your toolbar must be in top of views in your .xml file. It's working for me.
0

You should go with OnOptionsItemSelected()

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.badge:
            Toast.makeText(context, "Cart is Working", Toast.LENGTH_SHORT).show();
            Intent next = new Intent(context, ProductActivity.class);
            startActivity(next);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

For more information check this link: http://developer.android.com/guide/topics/ui/menus.html

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
0

May be the problem is you are not calling super.onOptionsItemSelected(item) when badge is clicked. Change your method as follows :-

@Override
public boolean onOptionsItemSelected(MenuItem item) {
       super.onOptionsItemSelected(item)
       switch (item.getItemId()) {
            case R.id.action_search:
                 break;
            case R.id.badge:
                 Toast.makeText(context, "Cart is Working", Toast.LENGTH_SHORT).show();
            /* Intent next = new Intent(context, ProductActivity.class);
                   startActivity(next);*/
                   break;
             default:
                   break;
            }
            return true;
     }
Frosty
  • 500
  • 4
  • 14
  • Could you post your xml? – Frosty Feb 27 '16 at 10:14
  • My xml is here..The following link. @Frosty – Parama Sudha Feb 27 '16 at 11:01
  • http://stackoverflow.com/questions/35622418/toolbar-menuitem-error/35622940?noredirect=1#comment58929371_35622940 – Parama Sudha Feb 27 '16 at 11:02
  • i checked your xml. I assume you must have given id of `RelativeLayout` in `badge_layout.xml` as `badge`. Instead of setting `android:clickable="true"` in `ImageView` set it on parent `RelativeLayout`.And also call `return super.onCreateOptionsMenu(menu);` instead of `return true` on `onCreateOptionsMenu` method. – Frosty Feb 27 '16 at 11:41
  • Even I use android:clickable="true" in Relative Layout,but didnt work. – Parama Sudha Feb 27 '16 at 11:49
  • Remove `android:clickable="true"` from `imageView` and try this as well:- And also call return `super.onCreateOptionsMenu(menu);` instead of `return true;` on `onCreateOptionsMenu` method. – Frosty Feb 27 '16 at 11:51