I am having trouble in implementing databinding in a Dialog. Is it possible?
Below is my xml.
<data> <variable name="olaBooking" type="com.example.myapp.viewmodels.ViewModel" /> </data> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.CardView android:id="@+id/cv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:elevation="4dp" android:padding="15dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorPrimary" android:gravity="center" android:padding="15dp" android:text="OLA Cab Booked !" android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="@color/colorPrimaryDark" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="start|center" android:padding="15dp" android:text="Car Details" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="@color/colorPrimaryDark" /> <TextView android:id="@+id/driverName" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="@{olaBooking.driverName}" /> <TextView android:id="@+id/carModel" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="@{olaBooking.getCarName}" /> <TextView android:id="@+id/carNo" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="@{olaBooking.getCabNo}" /> <TextView android:id="@+id/eta" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="5dp" android:text="@{olaBooking.getEta}" /> </LinearLayout> </android.support.v7.widget.CardView> </LinearLayout>
I want to bind the above layout in a Dialog. How is it possible? Below is my java code i tried but it's not working
dialog.setContentView(R.layout.dialog_ola_booking_confirmed); DialogOlaBookingConfirmedBinding binding = DataBindingUtil.inflate( LayoutInflater.from(dialog.getContext()), R.layout.dialog_ola_booking_confirmed, (ViewGroup) dialog.findViewById(R.id.cv), false); ViewModel viewModel = new ViewModel(this, event.olaBooking);
Data binding can also mean that if an outer representation of the data in an element changes, then the underlying data can be automatically updated to reflect the change. For example, if the user edits the value in a TextBox element, the underlying data value is automatically updated to reflect that change.
It is possible to use databinding in a Dialog, first to get the binding working on your Dialog you should inflate it first and pass it to the setContentView like this.
DialogOlaBookingConfirmedBinding binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()), R.layout. dialog_ola_booking_confirmed, null, false); setContentView(binding.getRoot());
Then you can pass the viewModel:
binding.setViewModel(new ViewModel(this, event.olaBooking));
And now you can see it working.
DataBindingUtil
for generated classes as said in Android Documentation You should use generated binding class's inflate
& bind
method (MyDialogBinding.inflate
).
public void showDialog(final Context context) { Dialog dialog = new Dialog(context); MyDialogBinding binding = MyDialogBinding.inflate(LayoutInflater.from(context)); dialog.setContentView(binding.getRoot()); dialog.show(); }
Binding Document says for DataBindingUtil
class's inflate method
.
Use this version only if layoutId is unknown in advance. Otherwise, use the generated Binding's inflate method to ensure type-safe inflation. DataBindingUtil.inflate(LayoutInflater.from(getContext()),R.layout.my_info_dialog_layout, null, false);
This is like finding binding generated class, when we have class already.
MyDialogBinding binding = MyDialogBinding.inflate(LayoutInflater.from(context));
or if you want make another class.
public class MyDialog extends Dialog { public MyDialog(@NonNull Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyDialogBinding binding = MyDialogBinding.inflate(LayoutInflater.from(getContext())); setContentView(binding.getRoot()); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With