0

In my application I have popup window with EditText field. If there is more one dot I want to invalidate positive button, mean to make it greyed out and unable to click. I looked on Java code and tried few solutions but I didn't figure out how to make it work. My code:

 fun dialogBuilder(){
        var dots = 0
        lateinit var inputText: String
        val builder = AlertDialog.Builder(this)
        builder.setTitle(getString(R.string.moneyStatus))
        val input = EditText(this)
        input.inputType = InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_NUMBER_FLAG_SIGNED
        input.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
        input.addTextChangedListener(object : TextWatcher {

            override fun afterTextChanged(s: Editable) {}

            override fun beforeTextChanged(s: CharSequence, start: Int,
                                           count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int,
                                       before: Int, count: Int) {

                val length = s.length
                if (length > 0){
                    for (i in 0..length){
                        if (i < length) {
                            if (s[i] == '.') {
                                dots += 1
                            }
                        }
                    }
            }
            }
        })
        builder.setView(input)
        builder.setPositiveButton("OK") {
            dialog, which -> inputText = input.text.toString()
            money.setText(inputText)
        }
        builder.setNegativeButton(getString(R.string.cancel)) {
            dialog, which -> dialog.cancel()
        }
        if (dots > 1){
            //gray out "OK" button
        }
        builder.show()
    }
}
CodeWithVikas
  • 1,105
  • 9
  • 33
Domin
  • 1,075
  • 1
  • 11
  • 28
  • 1
    One way would be adding custom positive/negative buttons and overriding them there. Or just creatiing your custom code for dialog. But as a simple solution, I would just add a check in onClick of positive button for number of dots and doing nothing when they are more than one – Lukas Anda Oct 24 '18 at 18:40
  • Thanks, that's also a way, but I think I'd love to invalidate it anyway to make user change his input. – Domin Oct 24 '18 at 18:54
  • While I search for solution, try to at least show a toast, that should be possible :) – Lukas Anda Oct 24 '18 at 18:56

2 Answers2

1

As I was googling, I stubled upon this question, that should answer your.

How to disable / enable dialog negative positive buttons?

I compiled the code to kotlin and it seems working, or at least compileable :D

fun dialogStuff() {
    val builder = AlertDialog.Builder(this)
    builder.setIcon(android.R.drawable.ic_dialog_info)
    builder.setTitle("Alert dialog title")
    builder.setMessage("This is the example code snippet to disable button if edittext attached to dialog is empty.")
    builder.setPositiveButton("PositiveButton"
    ) { arg0, arg1 ->
        // DO TASK
    }
    builder.setNegativeButton("NegativeButton"
    ) { arg0, arg1 ->
        // DO TASK
    }
    val input = EditText(this)
    val lp = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT)
    input.layoutParams = lp
    builder.setView(input)
    val dialog = builder.create()
    dialog.show()
    (dialog as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false
    input.addTextChangedListener(object : TextWatcher {
        override fun onTextChanged(s: CharSequence, start: Int, before: Int,
                                   count: Int) {
        }

        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int,
                                       after: Int) {
        }

        override fun afterTextChanged(s: Editable) {
            // Check if edittext is empty
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = !TextUtils.isEmpty(s)

        }
    })
}
Lukas Anda
  • 716
  • 1
  • 9
  • 30
  • I still have a problem. As I built a dialog using AlertDialog.Builder (there was no way to build it with AlertDialog) there is no reference to use `getButton()`. I figured out that in Kotlin I may use simply `AlertDialog.BUTTON_POSITIVE` what is (as I think) the same as `((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE)`. But afterwards I cannot use `setEnabled()`. Using `AlertDialog.builder` I may only add new buttons but I think not to override them. – Domin Oct 24 '18 at 19:13
  • 1
    Thanks! It compiled as well as it ran! The breaking point of the code is `val dialog = builder.create()` where variable is being initialized. – Domin Oct 24 '18 at 19:41
0

To disable a button:

 mButtonToggle.setEnabled( false );

To enable the button:

 mButtonToggle.setEnabled( true );
Ves
  • 387
  • 2
  • 10
  • The problem is that I can't figure out where to initialize variable in the code to disable it later. – Domin Oct 24 '18 at 18:53