Tony
Tony

Reputation: 2748

Create button drop-down menu

How to make a drop-down menu as image below?

enter image description here

I have a + button in my app. When it clicked, I want it shows drop-down menu under + button.

enter image description here

This is what I've tried so far. When the + button is clicked, the drop-down menu does not display.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Button addNew; // for + button

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

        addNew=(Button)findViewById(R.id.add);
        addNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu po=new PopupMenu(MainActivity.this,addNew); //for drop-down menu
                po.getMenuInflater().inflate(R.menu.popup_menu,po.getMenu());

                po.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
                {
                    public boolean onMenuItemClick(MenuItem item)
                    {
                       return true;
                    }
                });
            }
        });
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // 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.
       switch(item.getItemId()){
           case R.id.add:
       }
        return super.onOptionsItemSelected(item);
    }
}

popup_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/addForm"
        android:title="Create New "/>

    <item
        android:id="@+id/editForm"
        android:title="Edit File"/>


</menu>

Can someone give me a hints ? Thanks

Upvotes: 0

Views: 1433

Answers (1)

bond007
bond007

Reputation: 2494

You have not set visibility of PopMenu to Visible

Use

   po.show();

Upvotes: 1

Related Questions