Pop-up menu does not check radio Buttons

Asked

Viewed 31 times

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();
        }
    });

1 answer

0


The problem is that every time you click the button btntimer a new Popupmenu is created, causing the previous one to be lost as well as the Radiobutton "checked".

You must create Popupmenu outside the onClick() 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();
    }
});
  • 1

    Hit the fly! Heheh, so every time I clicked the button it called the reset menu? Thank you very much! It was all afternoon. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.