CheckBox checked / unchecked does not follow instructions

2

Activity starts with the layout activity_home and the CheckBox unchecked. When I touch% color, the layout becomes CheckBox , but activity_home_avancado remains unchecked. On the second touch, it is checked and > layout remains as the advanced one. On the third tap, the Checkbox is cleared, but the active layout remains being CheckBox . Why these two errors?

Follow the code:

public class HomeActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    final CheckBox checkBoxAvancado = (CheckBox) findViewById(R.id.modoavancado_cb);

    checkBoxAvancado.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (checkBoxAvancado.isChecked()) {
                setContentView(R.layout.activity_home_avancado);
            }
            else {
                setContentView(R.layout.activity_home);
            }
        }
    });


    } //fecha onCreate
}

And the same activity_home_avancado present in the two XML files:

<CheckBox
            android:id="@+id/modoavancado_cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:textColor="#7F7F7F"
            android:text="@string/modoavancado" />

(nothing appears in LogCat)

    
asked by anonymous 17.12.2014 / 04:36

1 answer

3

Although in each Layouts , CheckBox's have the same id same subject.

When you use the (CheckBox) findViewById(R.id.modoavancado_cb) method, it will look for cc-enabled in>, through setContentView()

When the listener is declared, for CheckedChanged , this is associated with CheckBox Layout R.layout.activity_home because that is the Layout that is currently associated with Activity

What is happening is that the code of onCheckedChanged() is only executed once. When you assign the Layout to Activity to

if (checkBoxAvancado.isChecked()) {
    setContentView(R.layout.activity_home_avancado);
}

CheckBox that now appears on the screen is defined in the Layout activity_home_driven activity_home .

As this CheckBox does not have any associated OnCheckedChangeListener faults, nothing happens when it is checked.

The setContentView should only be used once. To change the screen content create another Activity , use Fragments or hide / show Views Layout

    
17.12.2014 / 11:57