2

I have a CheckBox that enables/disables a couple of EditText views. At first, when the activity is started, I'm able to click on the EditText, but when I press the CheckBox (sets enabled to false) and then press the checkbox again(which supposedly sets the EditText to enabled), I can't click on the EditText.

This is the java code

    @Override
public void onCheckedChanged(CompoundButton buttonView,
        boolean isChecked) {
    final EditText etPersonTitle = (EditText)v.findViewById(R.id.et_person_title);
    final EditText etFirstname = (EditText)v.findViewById(R.id.et_first_name);
    final EditText etSurname = (EditText)v.findViewById(R.id.et_surname);

    if(isChecked){
        etPersonTitle.setText(R.string.unknown);
        etFirstname.setText(R.string.unknown);
        etSurname.setText(R.string.unknown);
    }

    else {
        etPersonTitle.setText("");
        etFirstname.setText("");
        etSurname.setText("");
    }

    etPersonTitle.setEnabled(!isChecked);
    etPersonTitle.setFocusable(!isChecked);
    etFirstname.setEnabled(!isChecked);
    etFirstname.setFocusable(!isChecked);
    etSurname.setEnabled(!isChecked);
    etSurname.setFocusable(!isChecked);
}

and here is the xml:

<TableRow
                android:id="@+id/tableRow1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/title" />

                <EditText
                    android:id="@+id/et_person_title"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ems="15"
                    android:inputType="textPersonName"
                    android:textAppearance="?android:attr/textAppearanceSmall" >
                </EditText>
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/first_name" />

                <EditText
                    android:id="@+id/et_first_name"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ems="15"
                    android:inputType="textPersonName"
                    android:textAppearance="?android:attr/textAppearanceSmall" >
                </EditText>
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:text="@string/surname" />

                <EditText
                    android:id="@+id/et_surname"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:ems="15"
                    android:inputType="textPersonName"
                    android:textAppearance="?android:attr/textAppearanceSmall" >
                </EditText>
            </TableRow>

            <LinearLayout
                android:id="@+id/ll_unknown_customer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"
                    android:text="@string/unknown_customer" />

                <CheckBox
                    android:id="@+id/cb_unknown_customer"
                    style="@style/checkbox_style"
                    android:background="@drawable/checkbox_background"
                    android:button="@null"
                    android:checked="false" />

            </LinearLayout>

one thing to note is that the TableLayout is inside a ScrollView. I'm not sure if that's what's causing this issue, but that's my first suspicion.

I tried to setClickable(true) but it doesn't work either.

Thanks in advance.

Vancert Staff
  • 205
  • 1
  • 3
  • 12
  • Can you try inverting your logic, so that it becomes setEnabled(isChecked); and setFocusable(isChecked) for the three EditTexts? And share the results if the issue remains the same or not. – dragi Sep 05 '14 at 14:24
  • First of all most coders don't use negative logic !!!!! and absolutely nobody likes to read supposedly in a question because this implies doubt that you haven't even tried. – danny117 Sep 05 '14 at 14:27
  • So the XML you provided with the TableLayout is the Layout of a single ListView-item? When using a custom ArrayAdapter with a ListView you're encountering the [`getView recycling principle`](http://stackoverflow.com/a/14108676/1682559) when the list is to large for the screen (and thus the ListView becomes scrollable). The problem most likely lies somewhere in the ArrayAdapter's getView-method. From the code provided I can't really spot the issue, but since I had quite some trouble with the getView recycler in the past I presume it also causes some problems here. – Kevin Cruijssen Sep 05 '14 at 14:56
  • danny, using setEnabled(!isChecked()) was used only to reduce the lines of code instead of having to write setEnabled(false) when the checkbox isChecked, and vice-versa. I'm only working in the project with one other person so I didn't know it was bad practice. I'll keep that in mind. And that is incorrect, I did try... I'm not sure how one thing leads to the other... – Vancert Staff Sep 05 '14 at 15:17
  • Kevin, I am terribly sorry!!! What I meant was "ScrollView", not Listview. That was a really bad mistake of mine.... I'll edit the question immediately. – Vancert Staff Sep 05 '14 at 15:18

2 Answers2

1

Do this.

if (isChecked) {
                etPersonTitle.setText("asd");
                etFirstname.setText("asd");
                etSurname.setText("asd");
                etPersonTitle.setEnabled(!isChecked);
                etPersonTitle.setFocusable(!isChecked);
                etFirstname.setEnabled(!isChecked);
                etFirstname.setFocusable(!isChecked);
                etSurname.setEnabled(!isChecked);
                etSurname.setFocusable(!isChecked);
            }

            else {
                etPersonTitle.setText("");
                etFirstname.setText("");
                etSurname.setText("");

                etPersonTitle.setEnabled(true);
                etPersonTitle.setFocusableInTouchMode(true);

                etFirstname.setEnabled(true);
                etFirstname.setFocusableInTouchMode(true);

                etSurname.setEnabled(true);
                etSurname.setFocusableInTouchMode(true);

            }
Moubeen Farooq Khan
  • 2,875
  • 1
  • 11
  • 26
  • This was the answer I was looking for. Can I ask why do I have to setFocusableInTouchMode instead of just setFocusable? – Vancert Staff Sep 05 '14 at 15:25
1

setFocusable mainly used for enable/disable view's focus event on both touch mode and keypad mode( using up/down/next key).

setFocusableInTouchMode mainly used for enable/disable view's focus event on touch mode alone.

If you are disabled setFocusable it also disabled the view's focus event on touch mode.

Giorgio
  • 1,973
  • 4
  • 36
  • 51
TiaN
  • 21
  • 4