views:

236

answers:

3

Hi all!

I´m using a checkbox in my code that when its checked it makes a textview and a editText visibles, but if I uncheck de checkbox they continue being visible instead of dissapear.

Here is the code:

final CheckBox save = (CheckBox) findViewById(R.id.checkbox);
        save.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {



                // Perform action on clicks, depending on whether it's now checked
                if (((CheckBox) v).isChecked()) {

                    nameText.setVisibility(1);
                    editName.setVisibility(1);

                } else {

                    nameText.setVisibility(0);
                    editName.setVisibility(0);

                }
            }
        });

And part of the xml which is inside an Relative Layout:

    <LinearLayout android:id="@+id/linearLayout3"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below = "@+id/linearLayout2">

    <TextView android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="@string/name" 
        android:visibility="invisible"/>
    <EditText android:id="@+id/name" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:visibility="invisible"/>    


    <CheckBox android:id="@+id/checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save" />
</LinearLayout>

What should i do to make the textView and EditText dissapear when i uncheck the checkbox?

Thank you!

+1  A: 

Use View.VISIBLE, View.INVISIBLE, View.GONE to control visibility (instead of 0 & 1).

alex
Thank you! It do the trick !
fxi
+1  A: 

Two things:

  1. You should use setOnCheckedChangeListener(), which will make your life easier.

  2. You should use View.GONE and View.VISIBLE instead of integers for setVisibility().

Daniel Lew
thank you for your answer!
fxi
A: 

the problem is in this part:

 // Perform action on clicks, depending on whether it's now checked
                if (((CheckBox) v).isChecked()) {

                    nameText.setVisibility(1);
                    editName.setVisibility(1);

                } else {

                    nameText.setVisibility(0);
                    editName.setVisibility(0);

                }

yout shouldn't use integer values, but instead use the constants provided by the view class

// Perform action on clicks, depending on whether it's now checked if (((CheckBox) v).isChecked()) {

                nameText.setVisibility(View.VISIBLE);
                editName.setVisibility(View.VISIBLE);

            } else {

                nameText.setVisibility(VIEW.INVISIBLE);
                editName.setVisibility(VIEW.INVISIBLE);

            }

instead of invisible you could also use GONE. then your invisible textedit doesn't need space in the layout

if you check the API you will see that 1 isn't a valid parameter:

http://developer.android.com/reference/android/view/View.html#INVISIBLE

Roflcoptr
thak you for your answer!
fxi