3

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()
    }
Lilya
  • 495
  • 6
  • 20
  • `But the app crashed. ` show error log. show your code – IntelliJ Amiya Aug 14 '20 at 10:45
  • Updated my question with error log. Please, take a look. Thank you! – Lilya Aug 14 '20 at 11:04
  • 1
    @IntelliJAmiya, added the code as well. Please, take a look. Thank you! – Lilya Aug 14 '20 at 11:07
  • 1
    @IntelliJAmiya is correct but importantly you have to use `.create()` when you instantiate the `alertDialog` as in: `val alertDialog = AlertDialog.Builder(context).create()`. Look at the use of `.create()` at the end. The callback will change too as in: `alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK") { dialog, _ -> }`. Make sure to look closely at IntelliJAmiya answer. – Lance Samaria Jun 01 '23 at 02:36

3 Answers3

3

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))
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • Hi @IntelliJ Amiya and thanks for the comment. I have tried putting after ```show()``` and it doesn't work. I can access ```getButton()``` only that way: ```dialog.create().getButton() ```, but it crashes anyway. – Lilya Aug 14 '20 at 10:59
  • Updated my question with error log. Please, take a look. Thank you! – Lilya Aug 14 '20 at 11:04
  • @Lilya check https://stackoverflow.com/a/33438415/3395198 – IntelliJ Amiya Aug 14 '20 at 11:06
  • I've just tried it and also have tried it before. It crashes with: ```java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setTextColor(int)' on a null object reference``` – Lilya Aug 14 '20 at 11:12
  • 1
    thank you SO much! It worked on the test project. I will implement this solution to my project and accept your answer as correct. Have a nice day! – Lilya Aug 14 '20 at 12:05
2
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.

Try this
  • 511
  • 3
  • 8
0

Try with custom dialog, Here is code snippet

 private fun showDialog(title: String) {
    val dialog = Dialog(activity)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(false)
    dialog.setContentView(R.layout.custom_layout)
    val body = dialog.findViewById(R.id.body) as TextView
    body.text = title
    val okBtn = dialog.findViewById(R.id.okBtn) as Button
    val noBtn = dialog.findViewById(R.id.noBtn) as TextView
    okBtn.setOnClickListener {
        dialog.dismiss()
    }
    noBtn.setOnClickListener { dialog.dismiss() }
    dialog.show()

}
Dhara Jani
  • 461
  • 3
  • 10
  • 2
    Hi @DharaJani and thanks for your answer. I will surely need it for later bc I'm planning to do a custom alert. Right now I'm using IntelliJ Amiya's answer. – Lilya Aug 14 '20 at 12:12