I'm trying to bind using LiveData the textColor and a view. In order to modify the view's color with the LiveData.
I've the following layout:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable name="fragment" type="com.package.RegisterFragment" />
<variable name="viewModel" type="com.package.RegistrationViewModel" />
</data>
[......]
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/password_field"
android:background="@drawable/rounded_edit_text"
android:layout_width="match_parent"
android:layout_height="45dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="@={viewModel.password}"
android:textColor="@{context.getResources().getColor(viewModel.passwordColor)}"
android:paddingRight="10dp"
android:paddingLeft="20dp"
android:inputType="textPassword"/>
And the following code in my fragment:
private var defaultTextColor: Int = android.R.color.black
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val fragmentBinding: FragmentRegisterBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_register, container, false)
fragmentBinding.fragment = this
fragmentBinding.viewModel
fragmentBinding.lifecycleOwner = this
convertView = fragmentBinding.root
viewModel = activity?.run {
ViewModelProviders.of(this)[RegistrationViewModel::class.java]
} ?: throw Exception("Invalid Activity")
(viewModel as RegistrationViewModel).emailColor.value = defaultTextColor
But my text color is a solid white. Anyone knows why?
I have to change my XML like this:
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/password_field"
android:background="@drawable/rounded_edit_text"
android:layout_width="match_parent"
android:layout_height="45dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="@={viewModel.password}"
android:textColor="@{viewModel.passwordColor}"
android:paddingRight="10dp"
android:paddingLeft="20dp"
android:inputType="textPassword"/>
And in my code I changed the color to:
private var defaultTextColor: Int = Color.BLACK
The missing viewModel actually doesn't effect the functioning of the app (really strange though).