ternary operator for drawable in layout expression


Ask A Question





Login To post a question

This is my fragment_setting.xml.

<ToggleButton
    android:layout_width="47dp"
    android:layout_height="27dp"
    android:background="@{settingVm.isNotiOn ? @drawable/btn_on_mid : @drawable/btn_off_mid}"
    android:onClick="@{()->settingVm.changeBtnStatus()}"
    android:text="@string/on"
    android:textOff="on"
    android:textOn="on"
    android:textSize="11sp"
    android:textStyle="bold" />

<ToggleButton
    android:layout_width="47dp"
    android:layout_height="27dp"
    android:background="@{settingVm.isNotiOn ? @drawable/btn_off_mid : @drawable/btn_on_mid}"
    android:onClick="@{()-> settingVm.changeBtnStatus()}"
    android:text="@string/off"
    android:textOff="off"
    android:textOn="off"
    android:textSize="11sp"
    android:textStyle="bold" />

This is my SettingViewModel

class SettingViewModel(handler: SettingHandler) : ViewModel() {
    var handler = handler
    var isNotiOn: Boolean? = true
    var visibility = View.VISIBLE

    init {
        if (BuildConfig.DEBUG) {
            Timber.plant(Timber.DebugTree())
            Timber.d("start")
        }
    }

    fun onBackBtnPressed() {
        Timber.d("onBackBtnPressed()")
        handler.onBackBtnPressed()
    }

    fun showLogoutDialogue() {
        Timber.d("showLogoutDialogue()")
        handler.showLogoutDialogue()
    }

    fun changeBtnStatus(){
        Timber.d("changeBtnStatus()")
        handler.changeBtnStatus()
    }
}

And this is my SettingFragment

...
        val spUtil = SharedPreferenceUtil(activity!!)
        when (spUtil.isNotificationOn) {
            false -> {
                binding!!.settingVm!!.isNotiOn = false
            }
            else -> {
                binding!!.settingVm!!.isNotiOn = true
            }
        }
...
    override fun changeBtnStatus() {
        // TODO: Set real notification setting.

        val spUtil = SharedPreferenceUtil(activity!!)
        when (binding!!.settingVm!!.isNotiOn) {
            true -> {
                binding!!.settingVm!!.isNotiOn = false
                spUtil.isNotificationOn = false
            }
            else -> {
                binding!!.settingVm!!.isNotiOn = true
                spUtil.isNotificationOn = true
            }
        }
    }

What’s the problem??? I am not using two way binding and the ternary operator like @={}. But I reckon I should use two way binding because it is not a constant value. And I have two images correctly.

Someone says I should not use is prefix because it might generate getter and setter. So, I even tried removing it and define has prefix or just NotiOn but didn’t work.

For more info please click herehttps://stackoverflow.com/q/56591078



Have a question or need a custom quote?
info@smartinfocare.com