I'm working on an Android app and encountering issues with TextEdit states.
I have many TextEdits in a grid, all using one of the following background selectors depending on whether the input is correct, incorrect, or not checked :
Neutral:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/focused_text" />
<item android:state_pressed="true" android:drawable="@color/pressed_text" />
<item android:drawable="@color/unfocused_text" />
Correct:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/pressed_text" />
<item android:state_focused="true" android:drawable="@drawable/correct_border_focused" />
<item android:drawable="@drawable/correct_border" />
Incorrect:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/pressed_text" />
<item android:state_focused="true" android:drawable="@drawable/incorrect_border_focused" />
<item android:drawable="@drawable/incorrect_border" />
The boxes are defined in a layout by:
<EditText
android:id="@+id/R11"
style="@style/QuestionNumbers"
android:layout_width="@dimen/structure_box_width"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text=" "
android:background="@drawable/input_background"
android:gravity="center"
app:layout_constraintTop_toBottomOf="@+id/NumBox1"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="@+id/NumBox1"
android:visibility="invisible"
/>
<EditText
android:id="@+id/R12"
style="@style/QuestionNumbers"
android:layout_width="@dimen/structure_box_width"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/input_background"
android:gravity="center"
app:layout_constraintTop_toBottomOf="@+id/NumBox2"
android:layout_marginLeft="0dp"
app:layout_constraintLeft_toLeftOf="@+id/NumBox2"
android:visibility="invisible"
/>
(There are many more, but they all have the same parameters except for different constraints).
They are all in a constraint layout with the following parameters:
<android.support.constraint.ConstraintLayout
android:id="@+id/StructureLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp"
android:layout_gravity="center_vertical">
I change the backgrounds, when necessary, by finding the R.drawable using:
Drawable normal_input = getResources().getDrawable(R.drawable.input_background);
and then setting :
rBoxes[i][j].setBackground(normal_input);
In context, that code is here:
checkAnswers = (Button) findViewById(R.id.hardExampleAnswerCheck);
checkAnswers.setVisibility(View.VISIBLE);
checkAnswers.setClickable(true);
checkAnswers.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Boolean any_incorrect = false;
Boolean finished = true;
// when Check Answers is pressed, go through the boxes that are enabled to see
// if the values match the ones in the answers
// if anything is empty or incorrect, note that
for (int i = 0; i<rBoxes.length ; i++) {
for (int j = 0; j < rBoxes[i].length; j++) {
if (rBoxes[i][j] != null) {
if (rBoxes[i][j].getVisibility()==View.VISIBLE && rBoxes[i][j].isEnabled()) {
if (rBoxes[i][j].getText().toString().equals(rBoxAnswers[i][j])) {
rBoxes[i][j].setBackground(correct_input);
} else {
rBoxes[i][j].setBackground(incorrect_input);
}
}
}
}
}
Where the RBoxes[][] are earlier initialized by:
rBoxes[1][1] = (EditText) findViewById(R.id.R11); // initialize and find the rBoxes
rBoxes[1][2] = (EditText) findViewById(R.id.R12);
rBoxes[2][1] = (EditText) findViewById(R.id.R21); // rBoxes are the boxes that are
rBoxes[2][2] = (EditText) findViewById(R.id.R22); // filled in during the structure
rBoxes[2][3] = (EditText) findViewById(R.id.R23);
///.... <for all of them>
When I press on a box, the color does indeed change from unfocused_text to pressed_text and then (usually but not always) to focused_text. It also does not always change back to unfocused_text when I press on another box. The effect is that I have multiple boxes that appear focused or pressed, even when the input only goes into one, as it should.
This happens regardless of whether the background was ever changed.
There does not seem to be any sort of deterministic set of boxes or inputs that behave this way, so I am really confused about what is going on. The actual focus (i.e. where the inputed text appears) changes as expected.
Any idea about what could be happening and how to fix?