Question

I want to make something like that:

gmail menu collapsible

I try to make the popup menu whit the collapseview, y the action bar. but i try all the thing that i found on internet and no solution found. i didnt need suppoart android api less than 15.

this the main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item android:id="@+id/action_search"
          android:icon="@android:drawable/ic_dialog_email"
          android:title="@string/hello_world"
          android:showAsAction="always|collapseActionView"
          />
    <item android:id="@+id/action_compose"
          android:icon="@android:drawable/btn_star"
          android:title="@string/hello_world" 
          android:showAsAction="never"
          />
    <item android:id="@+id/action_compose2"
          android:icon="@android:drawable/btn_star"
          android:title="@string/hello_world" 
          android:showAsAction="never"
          />
</menu>

This my activity:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar a = getActionBar();
        a.setTitle("Mariano");


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        MenuItem menuItem = menu.findItem(R.id.action_search);

        MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem arg0) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem arg0) {
                // TODO Auto-generated method stub
                return false;
            }
        });

        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return false;
    }
}
Was it helpful?

Solution

If I understood correctly you want to open an Android Popup Menu by click on a Button. Then all you have to do is to add this to your MainActivity:

 button1 = (Button) findViewById(R.id.button1);  
      button1.setOnClickListener(new OnClickListener() {  

       @Override  
       public void onClick(View v) {  
        //Creating the instance of PopupMenu  
        PopupMenu popup = new PopupMenu(MainActivity.this, button1);  
        //Inflating the Popup using xml file  
        popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());  

        //registering popup with OnMenuItemClickListener  
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {  
         public boolean onMenuItemClick(MenuItem item) {  
               int i = item.getItemId();
                        if (i == R.id.id1) {
                            //do something
                            return true;
                        }
                        else if (i == R.id.id2){
                            //do something
                        }
                        else if (i == R.id.id3) {
                            //do something
                            return true;
                        }
                        else if (i == R.id.id4) {
                            //do something
                            return true;
                        }else {
                            return onMenuItemClick(item);
                        }
        });  

        popup.show();//showing popup menu  
       }  
      });//closing the setOnClickListener method  
     }  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top