I have a toolbar with a menu that contains a custom button at the end and I want to change the text of this button according to the fragment's position in a view pager. But when I call the function that I used in the BindingAdapter which is showButtonText it doesn't being called
I already used this way with a text view and it's already worked but this customizes button within a given menu doesn't.
My code :
**BindingAdapter : **
@BindingAdapter("app:showButtonText")
fun showButtonText(buttonView: Button, resId: Int?) {
buttonView.text = resId?.let(buttonView.context::getString)
?: buttonView.context.getString(R.string.next)
}
**custom button xml : **
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="com.cupcake.jobsfinder.ui.create_job.CreateJobViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/next_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/background"
android:elevation="0dp"
android:stateListAnimator="@null"
**app:showButtonText="@{viewModel.jobUiState.titleToolBar}"**
android:textColor="@color/primary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:textAllCaps="false"
tools:text="Next"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
**menu file : **
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_next"
android:title="@string/next"
app:showAsAction="always"
app:actionLayout="@layout/custom_button"/>
</menu>
view model :
package com.cupcake.jobsfinder.ui.create_job
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.cupcake.jobsfinder.R
import com.cupcake.jobsfinder.domain.usecase.CreateJobUseCase
import com.cupcake.jobsfinder.ui.base.BaseViewModel
import com.cupcake.jobsfinder.ui.create_job.state.CreateJobUiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class CreateJobViewModel @Inject constructor(
private val createJob: CreateJobUseCase
) : BaseViewModel() {
private val _jobUiState = MutableStateFlow(CreateJobUiState())
val jobUiState = _jobUiState.asStateFlow()
private val _event = MutableLiveData<CreateJobEvent>()
val event: LiveData<CreateJobEvent> = _event
fun handleEvent(event: CreateJobEvent) {
when (event) {
is CreateJobEvent.PageScrolled -> {
onChangeIndexViewPager(event.index)
}
}
}
private fun onChangeIndexViewPager(index: Int) {
_jobUiState.update {
it.copy(
titleToolBar = getTitleToolBar(index)
)
}
}
private fun getTitleToolBar(index: Int): Int {
return if (index == 0) R.string.next else R.string.post
}
}
createJobFragment :
I found a blank button with no text just an empty button