views:

97

answers:

1

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 thel ist 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.

A: 

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!

pjz