I am having trouble in implementing databinding in a Dialog. Is it possible?
Below is my xml.
If you don't want to extend Dialog
, another possible solution could be:
Dialog dialog = new Dialog(this); // where "this" is the context
YourClassNameBinding binding = DataBindingUtil.inflate(dialog.getLayoutInflater(), R.layout.your_layout, null, false);
binding.setYourData(yourData);
dialog.setContentView(binding.getRoot());
dialog.show();
Hope it helps.
here is a full example of a AlertDialog with Databinding:
import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
public class MyDialog extends DialogFragment {
private static final String KEY_MY_INFO = "KEY_MY_INFO";
private String myInfo;
public static MyDialog newInstance(String myInfo) {
MyDialog dialog = new MyDialog();
Bundle bundle = new Bundle();
bundle.putString(KEY_MY_INFO, myInfo);
dialog.setArguments(bundle);
return dialog;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myInfo = getArguments().getString(KEY_MY_INFO);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
FragmentActivity activity = getActivity();
MyInfoBinding binding = DataBindingUtil.inflate(LayoutInflater.from(getContext()),
R.layout.my_info_dialog_layout, null, false);
binding.setMyInfo(myInfo);
return new AlertDialog.Builder(activity, R.style.AppCompatAlertDialogStyle)
.setView(binding.getRoot())
.create();
}
}
I tried @Dullahan's answer, however the dialog seemed to shrink strangely. So I tried another ways, finally found solution.
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/root"
android:layout_width="300dp"
android:layout_height="500dp">
<!-- ... -->
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
class CustomDialog(context: Context) : Dialog(context) {
private lateinit var binding: CustomDialogBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.custom_dialog)
binding = CustomDialogBinding.bind(findViewById(R.id.root))
}
}
DataBindingUtil
for generated classes as said in Android DocumentationYou 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());
}
}
**
**
The problem when you use databinding in DialogFragment is theme is not respect to dark mode colors. If you have problem with this use LayoutInflater.from(activity)
inside of LayoutInflater.from(context)
You can do the same without calling getRoot().
View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_delete_confirmation, null, false);
mBinding = DialogDeleteConfirmationBinding.bind(view);
mBinding.setViewModel(viewModel);
builder.setView(view);
builder.create();