0

My button has 3 states and the representation of each depends on a particular condition. (You can follow this question I've asked earlier to see the screenshots of what states my button has): Different background for a button's states in Kotlin

I want my button to kind of "block" its pressed state if it is not clickable.

My button's states:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_enabled="false" android:drawable="@drawable/background_stroke_bluish_rounded_corners_2dp"/>
    <item android:state_pressed="false"
            android:drawable="@drawable/background_bluish_rounded_corners" /> <!-- pressed -->
    <item android:state_pressed="true"
            android:drawable="@drawable/background_dark_teal_rounded_corners" /> <!-- focused -->
    <item android:state_enabled="true" android:drawable="@drawable/background_bluish_rounded_corners"/>

</selector>

How I tried to set the isClickable to false:

updateFragmentView?.mFragmentRootView?.btnUpdate?.isClickable = false

Right now it works and the needed event while isClickable = true doesn't happen, but what I want to implement is not to let the pressed state become visible if the button is not clickable. Is that possible to implement without changing the XML?

Something alike can be found in isUserInteractionEnabled for iOS. If set to false, the button's highlighted color doesn't work (if the button has one).

UPD: I've tried setEnabled(false) and also .isEnabled = true, but that does influence on the logic of my fragment, so that's not working for me.

Lilya
  • 495
  • 6
  • 20
  • 1
    I think what you are looking for is `setEnabled(false)` – Nongthonbam Tonthoi Aug 05 '20 at 15:31
  • Hi @NongthonbamTonthoi and thanks for the comment. I've tried this but that doesn't work with the logic of a fragment that I have. Please, see the updated question. Thanks! – Lilya Aug 05 '20 at 15:34
  • You already have the states of the button in your drawable in xml. So if you want to change to a different background you can set it programmatically as `button.background = //your drawable background`. Note that a button is not clickable when it is not enabled. – Nongthonbam Tonthoi Aug 05 '20 at 15:50
  • I believe this will interfere with the button's states in XML which work perfectly right now. I have also tried ```updateFragmentView?.mFragmentRootView?.btnUpdate?.isEnabled = false```, but that didn't help either. – Lilya Aug 05 '20 at 16:02
  • So what is it that you want exactly. If you want the button to be enabled but prevent click events then you have to intercept the touch event of the button like `button.setOnTouchListener({ view, motionEvent -> true })`, here returning true means that the button has consumed the touch event – Nongthonbam Tonthoi Aug 05 '20 at 16:10
  • If you saw, I attached the questions about the button's state I asked earlier. What I want is to block the button, so that it doesn't even show the pressed state. – Lilya Aug 05 '20 at 18:31

0 Answers0