Question

Trying to change the text on a button after it being pressed inside onClick() from XML. But the text wont update after the method is run, only after I click the button ones again it updates to the new text. I have tried running it using runOnUiThread(), but it acts the same. Anyone knows why?

Code:

public void setLabel(View v) {
    v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

    final EditText input = new EditText(this);

    new AlertDialog.Builder(this)
            .setMessage("Add label:")
            .setView(input)
            .setPositiveButton("Set", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    if (input.getText() == null || input.getText().toString().length() == 0) {
                        Toast.makeText(AlarmDialog.this, Html.fromHtml("<i>" + getString(R.string.label_reset) + "<i>"), Toast.LENGTH_SHORT).show();
                        labelSet = false;
                    }
                    else {
                        alarmLabel = input.getText().toString();
                        labelSet = true;
                        Toast.makeText(AlarmDialog.this, Html.fromHtml("<b>" + getString(R.string.label_set) + "<b>"), Toast.LENGTH_SHORT).show();
                    }

                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    Toast.makeText(AlarmDialog.this, Html.fromHtml("<i>" + getString(R.string.label_not_set) + "<i>"), Toast.LENGTH_SHORT).show();
                }
            }).create().show();

    Button button = (Button) v;
    if (labelSet)
        button.setText(R.string.label_set);
    else
        button.setText(R.string.label);
}

XML:

<Button
            android:id="@+id/label_button"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:text="@string/label"
            android:onClick="setLabel"
            style="@android:style/Widget.Holo.Button.Borderless"
            android:padding="8dp"/>
Was it helpful?

Solution 2

If labelSet is initially set to false, then the first time you click on the button...

button.setText(R.string.label);

...will be called, which wont update the text in the button.

Keep in mind that your onClick method for your AlertDialog will be called after your setLabel method runs to completion (specifically, it will be called when you click the positive button in the AlertDialog). I believe what you are looking for is something more like...

public void setLabel(View v) {
    v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);

    final Button button = (Button) v;
    final EditText input = new EditText(this);

    new AlertDialog.Builder(this)
        .setMessage("Add label:")
        .setView(input)
        .setPositiveButton("Set", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                if (input.getText() == null || input.getText().toString().length() == 0) {
                    Toast.makeText(AlarmDialog.this, Html.fromHtml("<i>" + getString(R.string.label_reset) + "<i>"), Toast.LENGTH_SHORT).show();
                    button.setText(R.string.label);
                }
                else {
                    alarmLabel = input.getText().toString();
                    Toast.makeText(AlarmDialog.this, Html.fromHtml("<b>" + getString(R.string.label_set) + "<b>"), Toast.LENGTH_SHORT).show();
                    button.setText(R.string.label_set);
                }

            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                Toast.makeText(AlarmDialog.this, Html.fromHtml("<i>" + getString(R.string.label_not_set) + "<i>"), Toast.LENGTH_SHORT).show();
            }
        }).create().show();
}

OTHER TIPS

i think the problem is that your

Button button = (Button) v;
if (labelSet)
    button.setText(R.string.label_set);
else
    button.setText(R.string.label);

code is executed before something is entered or clicked in the AlertDialog. Since the code is right below that line where you say AlertDialog.show(). So if you want the code to be executed afterwards you have to add it in the onClickListener. The Second time it only works because the alertview was closed before the button is clicked again. And your labelSet variable is already set.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top