【ANDROID STUDIO】 Popup Menu

package com.example.popup;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

private PopupMenu.OnMenuItemClickListener mOnMenuItemClickListener = new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_reply:
Toast.makeText(MainActivity.this, "Reply", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_reply_all:
Toast.makeText(MainActivity.this, "Reply All", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_forward:
Toast.makeText(MainActivity.this, "Forward", Toast.LENGTH_SHORT).show();
return true;
default:
return false;
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showPopupMenu(View view) {
PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
popupMenu.inflate(R.menu.menu_popup);
popupMenu.setOnMenuItemClickListener(mOnMenuItemClickListener);
popupMenu.show();
}
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageButton android:id="@+id/imageButtonReply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_revert"
android:onClick="showPopupMenu"
android:layout_marginLeft="8dp" />

</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_reply"
android:title="@string/menu_reply" />
<item android:id="@+id/menu_reply_all"
android:title="@string/menu_reply_all" />
<item android:id="@+id/menu_forward"
android:title="@string/menu_forward" />
</menu>

No comments:

When Bad People Make Good Art

I offer six guidelines on cancel culture ͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏     ­͏...

Contact Form

Name

Email *

Message *