I have MainActivity where I declare editTextValue as ObservableField I want to get it with editTextValue.get() on button click and pass it to another function. But I am always getting empty.
class MainActivity : AppCompatActivity() {
var editTextValue = ObservableField<String>("")
private lateinit var binding: ActivityMainBinding
private lateinit var chatViewModel: ChatViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(
this, R.layout.activity_main)
setContentView(binding.root)
sendButton.setOnClickListener {
println(editTextValue.get().toString()+"ABC")
if(editTextValue.get()!!.isEmpty()){
println("WHY IS EMPTY")
return@setOnClickListener
}
}
and In my xml layout I have defined a variable activity and set the text of editText as,
<?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="activity"
type="com.example.ashishgpt.MainActivity" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1C1B1F"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:background="#333333"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="AshishGpt"
app:titleTextColor="#FFFFFF" />
<EditText
android:id="@+id/editTextText"
android:layout_width="364dp"
android:layout_height="53dp"
android:layout_marginStart="1dp"
android:layout_marginEnd="3dp"
android:layout_marginBottom="2dp"
android:background="#333333"
android:ems="10"
android:hint="Enter your prompt here"
android:text="@={activity.editTextValue}"
android:inputType="text"
android:paddingStart="10dp"
android:textColor="#FBFBFB"
android:textColorHint="#D1CACA"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/imageButton"
app:layout_constraintStart_toStartOf="parent" />
No matter what I type in textenter code herefield, it prints empty on button click. Is there a point I am missing?
Is there a reason my observableFIeld showing empty value?
Yes. You have not actually bound the observableField. Thus, it does not update.
Your layout takes an Activity
as a variable which you have not passed through. Review the documentation on binding data.
binding = DataBindingUtil.setContentView(
this, R.layout.activity_main)
setContentView(binding.root)
binding.activity = this // Set the "activity" variable on the binding
As was declared in your xml layout file:
<data>
<variable
name="activity"
type="com.example.ashishgpt.MainActivity" />
</data>
If you don't pass the variable that was declared in the layout file, there's no work (i.e., binding) for DataBinding to do.
Perhaps you thought the call to DataBindingUtil.setContentView(this, ...)
was setting the variable, but that is just a convenience to inflate the view and create a binding class in one step.
P.S. - You probably don't want to be passing the Activity reference to the binding. You should be passing some POJO or data class
that represents the view state.