little_star
Lurker
Hi,
I am trying to create a small app as part of my learning experience in Android.
I got this code to show and hide a text using a checkbox
How can I control 2 different text with one checkbox (show one text and hide another and vice-versa)
Can someone please help me to make this work
I am trying to create a small app as part of my learning experience in Android.
I got this code to show and hide a text using a checkbox
How can I control 2 different text with one checkbox (show one text and hide another and vice-versa)
Can someone please help me to make this work
Code:
Import ...
public class MainActivity extends AppCompatActivity {
private TextView txtHelloWorld;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckBox checkBoxVisibility = findViewById(R.id.checkBox_visibility);
txtHelloWorld = findViewById(R.id.txtHelloWorld);
boolean isChecked = checkBoxVisibility.isChecked();
updateTextVisibility(isChecked);
checkBoxVisibility.setOnClickListener(v -> {
boolean isChecked1 = ((CheckBox)v).isChecked();
updateTextVisibility(isChecked1);
});
}
private void updateTextVisibility(boolean isChecked) {
if (isChecked) {
txtHelloWorld.setVisibility(View.VISIBLE);
} else {
txtHelloWorld.setVisibility(View.INVISIBLE);
}
}
}