-1

In Android Studio I have a Kotlin project with a button that sends an SMS message. When the Send SMS permission for the app is denied, the button is disabled. When a user clicks the disabled button, I would like to display a toast message to the user:

Toast.makeText(this, R.string.smsDisabled, Toast.LENGTH_LONG).show()

However, after much research I haven't been able to make this disabled button clickable. I have tried things like smsButton.isClickable = true on the button, but it still doesn't work. If it is possible to style the button to simply look like it is disabled, that would work too. My button style is currently set to @style/Widget.MaterialComponents.Button and I haven't had any success in changing the button colors. I have tried applying a background, backgroundTint, custom theme, and creating a custom background like in this question.

I am new to Android Studio so I have limited knowledge and experience. Is there a way to achieve this functionality?

SpencerLS
  • 361
  • 5
  • 16
  • 1
    Disabled means not clickable. Instead of disabling it, just change its style so it has a different color. – Tenfour04 Jul 05 '21 at 16:34
  • You don't necessary need to disable the button, just change the style to emulate a disabled stated. Handle the validation on the onClickListener and when you create the button the first time, on the oncreate in your activity/fragment. Also if you can't change the background color that's another problem. – javdromero Jul 05 '21 at 16:34
  • @javdromero As I stated, I also don't know how to style the button. Should I refactor the question to be more focused on this? – SpencerLS Jul 05 '21 at 16:39
  • @Tenfour04 Oh, that makes sense. As I said in the question, I am having trouble styling the button as well. Should the question be more about that? – SpencerLS Jul 05 '21 at 16:41
  • 2
    You should probably read the documentation first, there's a lot of it that talks about styling a `MaterialButton`: https://material.io/components/buttons/android#theming-buttons – Henry Twist Jul 05 '21 at 16:45

2 Answers2

2

Here is what you can do:

if (isPermissions) {
        //keep the button as it is or do what you have to when button is enabled
    } else {
        //change the button color and maybe icon two
        button.setBackgroundColor(ContextCompat.getColor(applicationContext, R.color.grey))
        button.setOnClickListner {
            //show toast
            Toast.makeText(this, R.string.smsDisabled, Toast.LENGTH_LONG).show()
        }
    }
Haris
  • 372
  • 3
  • 16
-1

isClickable only makes it clickable once again if the click is disabled with the same attribute.

Try:

button.isEnabled = true
Yash Joshi
  • 557
  • 7
  • 25