Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dialog.getButton() is not reachable in Android Kotlin

I am trying to change the color of the system alert dialog's buttons to a custom color. I have been trying to use this:

dialog.create().getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(getColor(R.color.bluish_grey))

But the app crashed. Also, dialog.getButton doesn't work. There are only set...() methods.

What's more, I have also tried using .getButton() after dialog.show(), but the app crashes with the error log below.

My alert dialog is not custom, it's just a system dialog. So, is this possible to set the alert dialog's buttons' color to a custom one without using XML?

This is what I want to achieve. My desired result

This is what I currently have: The current result

UPDATED: Error log:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: no.company.app, PID: 26643
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setTextColor(int)' on a null object reference
        at no.meshtech.demokit.view.activity.MainActivity.showCancelDialog(MainActivity.kt:402)
        at no.meshtech.demokit.view.activity.MainActivity.startUpdate(MainActivity.kt:317)
        at no.meshtech.demokit.view.activity.MainActivity.onClick(MainActivity.kt:67)
        at android.view.View.performClick(View.java:7869)
        at android.widget.TextView.performClick(TextView.java:14958)
        at android.view.View.performClickInternal(View.java:7838)
        at android.view.View.access$3600(View.java:886)
        at android.view.View$PerformClick.run(View.java:29362)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:8019)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

UPDATED 2:

My code for creating an alert:

   override fun showCompletedDialog() {
        DfuBaseService.PROGRESS_COMPLETED

        val dialog = AlertDialog.Builder(this)

            .setTitle(getString((R.string.dfu_completed_title)))
            .setMessage(getString(R.string.dfu_completed_message))
            .setPositiveButton(R.string.dfu_completed_ok) { dialog, _ ->
                transaction(listFragment, R.id.cvCentral)
                supportFragmentManager.findFragmentById(R.id.upperContainer)?.let {
                    supportFragmentManager.beginTransaction().remove(it).commit()
                    setNavigationTitle(getString(R.string.title_header_devices))
                }
                upperContainer.visibility = View.GONE

                Handler().postDelayed({
                    onBackPressed()
                    dialog.dismiss()
                }, 400)
            }


        dialog.setCancelable(false)
        dialog.show()
    }
like image 899
Lilya Avatar asked Dec 06 '25 04:12

Lilya


2 Answers

You should add setTextColor after show();

    val alertDialog = AlertDialog.Builder(this).create()
    alertDialog.setTitle(getString((R.string.app_name)))
    alertDialog.setMessage(getString(R.string.app_name))
    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "RESUME"
    ) { dialog, which -> dialog.dismiss() }

    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "ABORT"
    ) { dialog, which -> dialog.dismiss() }

    alertDialog.show()
    alertDialog.setCancelable(false)
    alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(ContextCompat.getColor(this,R.color.colorPrimary))
    alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(ContextCompat.getColor(this,R.color.colorAccent))
like image 114
IntelliJ Amiya Avatar answered Dec 07 '25 19:12

IntelliJ Amiya


val dialog = AlertDialog.Builder(this)
    .setTitle("Title")
    .setMessage("Message")
    .setPositiveButton("Positive") { dialog, _ ->
        // Do stuff
    }.setNegativeButton("Negative"){ dialog, _ ->
        // Do stuff
    }.create()

dialog.setCancelable(false)

dialog.setOnShowListener {
    dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(resources.getColor(R.color.colorPrimaryDark))
}

dialog.show()

You should use AlertDialog.Builder#create to first create alert and then set #setOnShowListener in which you can update the text color.

like image 33
Try this Avatar answered Dec 07 '25 18:12

Try this