Kishan Kumar
Kishan Kumar

Reputation: 709

How to achieve the below mentioned display in android?

enter image description here

My problem is that I want to achieve a display in which on clicking the dotted button on side of every song I want a display to pop up like in the second image shown below?enter image description here

Upvotes: 0

Views: 65

Answers (1)

Damini Mehra
Damini Mehra

Reputation: 3347

To create POPUp menu on button click:

list_item.xml:

<Button  
    android:id="@+id/button1"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_alignParentLeft="true"  
    android:layout_alignParentTop="true"  
    android:layout_marginLeft="62dp"  
    android:layout_marginTop="50dp"  
    android:text="Show Popup" />

It contains three items as show below. It is created inside the res/menu directory. File: poupup_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >  

<item  
    android:id="@+id/one"  
    android:title="PlayNext"/>  

<item  
    android:id="@+id/two"  
    android:title="AddtoPlayList"/>  

<item  
    android:id="@+id/three"  
    android:title="Add to Queue"/>  
</menu>  

It displays the popup menu on button click. File: MainActivity.java

private Button button1;  

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

    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) {
                    Toast.makeText(
                        MainActivity.this,
                        "You Clicked : " + item.getTitle(),
                        Toast.LENGTH_SHORT
                    ).show();
                    return true;
                }
            });

            popup.show(); //showing popup menu
        }
    }); //closing the setOnClickListener method
}

Upvotes: 3

Related Questions