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