Pop-up menu does not check the radio buttons

0

Menu:

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

    <item
        android:id="@+id/one"
        android:title="One"        
        />
    <item
        android:id="@+id/two"
        android:title="Two"
        tools:ignore="AppCompatResource" />
    <item
        android:id="@+id/three"
        android:title="Three"
        />
    </group>

</menu>

Java:

btntimer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popup = new PopupMenu(ChuvaLeveActivity.this, btntimer);
            popup.inflate(R.menu.action);
            // This activity implements OnMenuItemClickListener
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.one:
                            Toast.makeText(getApplicationContext(), "ONE", Toast.LENGTH_SHORT).show();
                            if (item.isChecked()) item.setChecked(false);
                            else item.setChecked(true);
                            return true;
                        case R.id.two:
                            Toast.makeText(getApplicationContext(), "TWO", Toast.LENGTH_SHORT).show();
                            if (item.isChecked()) item.setChecked(false);
                            else item.setChecked(true);
                            return true;
                        case R.id.three:
                            Toast.makeText(getApplicationContext(), "THREE", Toast.LENGTH_SHORT).show();
                            if (item.isChecked()) item.setChecked(false);
                            else item.setChecked(true);
                            return true;
                        default:
                            return false;
                    }
                }
            });
            popup.show();
        }
    });
    
asked by anonymous 14.06.2017 / 16:11

1 answer

0

The problem is that every time you click the btntimer button a new PopupMenu is created, causing the previous one to be lost just like the "Checked" RadioButton.

You should create the PopupMenu out of the onClick() of the button:

final PopupMenu popup = new PopupMenu(ChuvaLeveActivity.this, btntimer);
popup.inflate(R.menu.action);
// This activity implements OnMenuItemClickListener
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.one:
                Toast.makeText(getApplicationContext(), "ONE", Toast.LENGTH_SHORT).show();
                if (item.isChecked()) item.setChecked(false);
                else item.setChecked(true);
                return true;
            case R.id.two:
                Toast.makeText(getApplicationContext(), "TWO", Toast.LENGTH_SHORT).show();
                if (item.isChecked()) item.setChecked(false);
                else item.setChecked(true);
                return true;
            case R.id.three:
                Toast.makeText(getApplicationContext(), "THREE", Toast.LENGTH_SHORT).show();
                if (item.isChecked()) item.setChecked(false);
                else item.setChecked(true);
                return true;
            default:
                return false;
        }
    }
});

btntimer.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        popup.show();
    }
});
    
14.06.2017 / 22:37