What's new
DroidForums.net | Android Forum & News

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Checkbox activity to show/hide multiple texts

geojo

New Member
Hi,
I am trying to learn the basics of android with Java and got struck halfway creating a simple app.
This checkbox code works for single text and can someone help me make one text visible and hide second one.
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);
        }
    }
}
 
Back
Top