Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable a button in Kotlin?

Tags:

android

kotlin

I have created 2 activities, well the first one I use it as splash screen, now in the second one I have difficulties when deactivating a button, I leave the code for your understanding

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".TemaCalcActivity">

    <Spinner
            android:layout_width="144dp"
            android:layout_height="39dp"
            android:id="@+id/spnDiferencia" app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginStart="8dp" app:layout_constraintStart_toEndOf="@+id/spnMP"
            android:layout_marginEnd="8dp" android:layout_marginTop="24dp"
            app:layout_constraintTop_toBottomOf="@+id/textView5"/>

    <Button
            android:text="OK"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/button" app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="120dp"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="120dp" android:layout_marginTop="32dp"
            app:layout_constraintTop_toBottomOf="@+id/spnMP" android:background="@drawable/button_effect"
            android:textSize="18sp" android:textStyle="bold" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintVertical_bias="0.026" android:onClick="onBotonFinalizar" android:enabled="false"/>

</android.support.constraint.ConstraintLayout>

Main2Activity.kt

class Main2Activity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)

        spnDiferencia.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                when (position) {
                    0 -> { button.isEnabled = true}
                    1 -> { button.isEnabled = false}
        }
}

The error I have is that the entire application is stopped and then restarted, making this a repetition.

button.isEnabled = true
// or
button.isClickable = true

caution ---> button == btnCalcular

--- error log ---

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mrl.fr.tuto, PID: 22066
    java.lang.IllegalStateException: btnCalcular must not be null
        at com.mrl.fr.tuto.Main2Activity$onCreate$5.onItemSelected(Main2cActivity.kt:128)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:919)
        at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:908)
        at android.widget.AdapterView.access$300(AdapterView.java:53)
        at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:878)
        at android.os.Handler.handleCallback(Handler.java:815)
        at android.os.Handler.dispatchMessage(Handler.java:104)
        at android.os.Looper.loop(Looper.java:194)
        at android.app.ActivityThread.main(ActivityThread.java:5631)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

like image 484
royer Avatar asked Apr 12 '19 05:04

royer


2 Answers

Just to disable a button .

button.isEnabled = false
button.isClickable = false

and If you want to disable a button , and want to grey out its color & background ,you can do something like this -

fun markButtonDisable(button: Button) {
    button?.isEnabled = false
    button?.setTextColor(ContextCompat.getColor(textView.context, R.color.white))
    button?.setBackgroundColor(ContextCompat.getColor(textView.context, R.color.greyish))
}
like image 158
Sachin Avatar answered Nov 15 '22 06:11

Sachin


Did you try this

myButton.setEnabled(false);
like image 40
Madhusudhan Aradya Avatar answered Nov 15 '22 06:11

Madhusudhan Aradya