Skip to content

Instantly share code, notes, and snippets.

@conradKE
Last active December 22, 2020 11:04
Show Gist options
  • Save conradKE/d626cb25e461014fff25de934ad852f9 to your computer and use it in GitHub Desktop.
Save conradKE/d626cb25e461014fff25de934ad852f9 to your computer and use it in GitHub Desktop.
Enable Disable button with Checkbox

#Enable Disable Button With Checkbox

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

           //get checkbox by id && button
          val ckb_terms = findViewById<CheckBox>(R.id.checkbox_terms)
          val btnStarted = findViewById<Button>(R.id.btn_getstarted)
      

          //disabling button by default
           btnStarted.isEnabled = false

        //checking if checkbox is checked to move to the next activity
        ckb_terms.setOnCheckedChangeListener { buttonView, isChecked ->
            btnStarted.isEnabled = buttonView.isChecked
        }
        //moving to the next activity
            btnStarted.setOnClickListener {
                val intent = Intent(this, HomeActivity::class.java)
                startActivity(intent)
            }



        }


    }

#Here layout are xml file

       android:id="@+id/btn_getstarted"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Get Started"/>

   <com.google.android.material.checkbox.MaterialCheckBox
       android:id="@+id/checkbox_terms"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="agree to terms and conditions" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment