How can I get working dynamic ToggleButton text under android?
Asked Answered
E

1

16

I've got a ToggleButton that's set up like:

final ToggleButton filterButton = (ToggleButton) findViewById(R.id.filterTags);
        filterButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (filterButton.isChecked()) {
                    // pop up the list of tags so the user can choose which to filter by
                    // once one is chosen, the spinner will be updated appropriately
                    showDialog(DIALOG_TAGS);
                } else {
                    // going unpressed, set the the spinner list to everything
                    updateSpinner(db.itemNames());
                }
            }
        });

and the dialog looks like:

   case DIALOG_TAGS:
        final String[] tagNames = db.tagNamesInUse();
        dialog = new AlertDialog.Builder(this)
            .setItems(tagNames, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        updateSpinner(db.getItemNamesForTag(tagNames[which]));
                        final ToggleButton filterButton = (ToggleButton) findViewById(R.id.filterTags);
                        filterButton.setTextOn(tagNames[which]);
                        dialog.dismiss();
                    }
                })
                .setNegativeButton("Cancel", UITools.getDialogCancellingListener())
            .create();

The idea is: if the ToggleButton is turned on, it pops up a single-choice listview dialog that's the list of tags. Once a tag is chosen, it becomes the new textOn for the ToggleButton. If the ToggleButton is turned off (unChecked), then the text reverts to the static TextOff.

The problem is: the button isn't being redrawn once the dialog goes away. The text showing is still the previous value of textOn.

How can I force a redraw? I tried filterButton.postInvalidate(); but that didn't help.

Etymology answered 24/9, 2010 at 4:57 Comment(0)
E
20

Solved! Judicious reading of the source to ToggleButton shows that while setTextOn() and setTextOff() don't cause a call to (private) syncTextState which updates the TextView bits, calling setChecked() does. So the trick is:

dialog = new AlertDialog.Builder(this)
            .setItems(tagNames, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        updateSpinner(db.getItemNamesForTag(tagNames[which]));
                        final ToggleButton filterButton = (ToggleButton) findViewById(R.id.filterTags);
                        filterButton.setTextOn(tagNames[which]);
                        filterButton.setChecked(filterButton.isChecked());
                        dialog.dismiss();
                    }
                })

Which worked quite nicely. Yay for open source!

Etymology answered 25/9, 2010 at 4:58 Comment(1)
You are definitely right about syncTextState not being called. But, you could call setText(tagNames[which]) directly to change the text.Sevigny

© 2022 - 2024 — McMap. All rights reserved.