I currently have four buttons. The fourth button is a reset button I added to reset the activity to the state when it was originally started. However, when I added the reset button it disabled one of the buttons permanently, even though previously, the buttons were being enabled/disabled the way they were supposed to. Here is the relevant code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
// non relevant code
okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(this);
changeButton = (Button) findViewById(R.id.change_button);
changeButton.setOnClickListener(this);
nextButton = (Button) findViewById(R.id.next_button);
nextButton.setEnabled(false);
nextButton.setOnClickListener(this);
reset = (Button) findViewById(R.id.reset);
reset.setEnabled(false);
reset.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ok:
changeButton.setEnabled(false);
okButton.setEnabled(false);
nextButton.setEnabled(true);
break;
case R.id.change_button:
changeButton.setEnabled(false);
okButton.setEnabled(false);
nextButton.setEnabled(true);
break;
case R.id.next_button:
nextButton.setEnabled(false);
okButton.setEnabled(true);
changeButton.setEnabled(true);
break;
case R.id.reset:
Intent intent = getIntent();
finish();
startActivity(intent);
break;
default:
break;
}
}
Everything was working as it should have until all of the parts involving the reset button were added. Essentially, the behaviour I want is:
-
Initially: next and reset are disabled, they become enabled when either change or ok are clicked
-
When either change or ok are clicked, they both are disabled (to prevent clicking more than once) and both reset and next are enabled
-
When next or reset are clicked, they become disabled and change and ok become enabled.
The change, ok, and next button behaviors were working until the reset code was added. Then the next button became permanently disabled. What is wrong? How do I fix it?
EDIT*:
Here is the xml code for the buttons:
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/change_button"
android:layout_alignBottom="@+id/change_button"
android:layout_toLeftOf="@+id/reset"
android:text="@string/Ok" />
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/change_button"
android:layout_alignBottom="@+id/change_button"
android:layout_alignParentLeft="true"
android:text="@string/next_button" />
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/reset" />
<Button
android:id="@+id/change_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="14dp"
android:layout_toRightOf="@+id/next_button"
android:text="@string/change" />
This worked for me/ Add those lines.