Home > Software engineering >  Enable button if checkbox is selected in Kotlin?
Enable button if checkbox is selected in Kotlin?

Time:03-17

I want to enable a button if checkbox is selected or checked and disable it if unselected or unchecked. I started all my application using kotlin. if there is a way for doing somehting like that in kotlin please help! and if there is no way in kotlin, show me a way for doing that wanted I mentioned above. thanks to whome is trying to help. see this picture of my mean

here is my button and checkbox codes and :

<LinearLayout
        android:id="@ id/llIAWith"
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:orientation="horizontal"
        android:padding="10dp">

        <CheckBox
            android:id="@ id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/i_agree_with"
            android:textSize="15dp"
            android:textColor="@color/Middle_Gray"
            app:buttonTint="@color/Orange"
            tools:ignore="TextContrastCheck" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/terms_of_services"
            android:textSize="15dp"
            android:textColor="@color/Orange"
            tools:ignore="TextContrastCheck" />
    </LinearLayout>

    <androidx.appcompat.widget.AppCompatButton
        android:id="@ id/btnSUp"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/custom_button"
        android:text="@string/sign_up"
        android:textSize="18dp"
        android:textAllCaps="false"
        android:textColor="@color/White"
        tools:ignore="TextContrastCheck,TouchTargetSizeCheck" />

here is my kotlin codec :

class LoginActivity : AppCompatActivity() {
private lateinit var binding: ActivityLoginBinding
private lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityLoginBinding.inflate(layoutInflater)
    val view = binding.root
    setContentView(view)
    initData()
}
private fun checkForInternet(context: Context): Boolean {
    val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val network = connectivityManager.activeNetwork ?: return false
    val activeNetwork = connectivityManager.getNetworkCapabilities(network) ?: return false
    return when{
        activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
        activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
        activeNetwork.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) -> true
        else -> false
    }
}
private fun initData(){
    auth = FirebaseAuth.getInstance()
    clickListener()
    clickListenerFP()
}
private fun clickListenerFP(){
    binding.llFPassword.setOnClickListener{
        startActivity(Intent(this,ForgotPasswordActivity::class.java))
        finish()
    }
}
private fun clickListener(){
    binding.llDHAAccount.setOnClickListener{
        startActivity(Intent(this,RegisterActivity::class.java))
        finish()
    }
    binding.btnSIn.setOnClickListener{
        getUserData()
    }
}
private fun getUserData(){
    val email = binding.etEmail.text.toString()
    val password = binding.etPassword.text.toString()
    if(email.isNotEmpty() && password.isNotEmpty()){
        authUser(email, password)
    }else{
        Toast.makeText(this,resources.getString(R.string.all_inputs_are_required),Toast.LENGTH_SHORT).show()
    }
}
private fun authUser(email: String, password: String){
    auth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener{
            checkResult(it.isSuccessful)
        }
}
private fun checkResult(isSuccess: Boolean){
    if (checkForInternet(this)) {
        Toast.makeText(this,resources.getString(R.string.connected), Toast.LENGTH_SHORT).cancel()
    } else {
        Toast.makeText(this,resources.getString(R.string.no_network_connection), Toast.LENGTH_SHORT).show()
    }
    if(isSuccess){
        startActivity(Intent(this,DashboardActivity::class.java))
        finish()
    }else{
        Toast.makeText(this,resources.getString(R.string.authentication_failed),Toast.LENGTH_SHORT).show()
    }
}
private var backPressedTime:Long = 0
private lateinit var backToast:Toast
override fun onBackPressed() {
    backToast = Toast.makeText(this,resources.getString(R.string.press_back_again_to_leave_the_app), Toast.LENGTH_SHORT)
    if (backPressedTime   2000 > System.currentTimeMillis()) {
        backToast.cancel()
        super.onBackPressed()
        return
    } else {
        backToast.show()
    }
    backPressedTime = System.currentTimeMillis()
}

CodePudding user response:

here it is

binding.cb.setOnCheckedChangeListener { _, isChecked ->
    binding.btnSUp.isEnabled = isChecked
}
  • Related