Jobish George
Jobish George

Reputation: 15

Opening new activities from Popup menu items

I'm having a popup menu on click of action bar button. When i click on the action bar button I'm getting my popup window. But i want to open another activities on clicking the popup menu items. How could i do that?

Following are my code snippets.

@Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.main, menu);
            return true;
        }
        @SuppressLint("NewApi") @Override
        public boolean onOptionsItemSelected(MenuItem item) {

            View menuItemView = findViewById(R.id.action_button); 
            PopupMenu popupMenu = new PopupMenu(this, menuItemView); 
            popupMenu.inflate(R.menu.popup);
            popupMenu.show();         
            return true;
    }

and my popup menu is as follows,

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <item
        android:id="@+id/one"
        android:title="About"
        android:visible="true"
        android:showAsAction="ifRoom|withText"/>

    <item
        android:id="@+id/two"
        android:title="Contact Us"
        android:visible="true"
        android:showAsAction="ifRoom|withText"/>

</menu>

What i want to do is, when i click on these menu items another activities has to be opened. How could i do that? Can someone help me please. Thanks in advance.

Upvotes: 0

Views: 2051

Answers (3)

SaravInfern
SaravInfern

Reputation: 3388

Use the id to launch the activity using switch statement with menu itemId

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.one:
            Intent intent1 =new Intent(this,ActivityOne.class);//firstActivity
            startActivity(intent1);
            return true;
        case R.id.two:
            Intent intent2 =new Intent(this,ActivityTwo.class);//second Activity
            startActivity(intent2);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

Upvotes: 1

Tapan Kumar Patro
Tapan Kumar Patro

Reputation: 767

Try this

popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        Toast.makeText(getApplicationContext(),
                                item.getTitle(), Toast.LENGTH_SHORT).show();
                        return true;
                    }
                });

Upvotes: 0

Saurav Prakash
Saurav Prakash

Reputation: 654

To open activity on popup menu click:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
        case R.id.menu_item1:
            Intent intent = new Intent(this, ActivityForItemOne.class);
            this.startActivity(intent);
            break;
        case R.id.menu_item2:
            // another startActivity, this is for item with id "menu_item2"
            break;
        default:
            return super.onOptionsItemSelected(item);
        }

        return true;
    }

Upvotes: 0

Related Questions