11

I have implemented DataBinding in Activity, Fragment and RecyclerView. Now trying to do it in Dialog, but little bit confuse about how to set custom view inside it?

Here is code i have implemented for Dialog.

Dialog dialog = new Dialog(context);
dialog.getWindow();

dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

LayoutTermsBinding termsBinding;

dialog.setContentView(R.layout.layout_terms);
dialog.getWindow().setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

dialog.show();

I know if it is Activity we can perform DataBindingUtil.setContentView() and for Fragment we can perform DataBindingUtil.inflate() but i am confuse about how to convert dialog.setContentView(R.layout.layout_terms); with DataBinding.

Ravi
  • 34,851
  • 21
  • 122
  • 183

1 Answers1

9

Assuming something like this is your layout_terms.xml:

<layout>
    <data>
        <!--You don't even need to use this one, this is important/necessary for the inflate method -->
        <variable name="testVariable" value="String" /> 
    </data>
    <LinearLayout>
        <TextView />
    </LinearLayout>
</layout>

First, you'll need to get your Binding. This is done by simply inflating it:

/*
* This will only work, if you have a variable or something in your 'layout' tag, 
* maybe build your project beforehand. Only then the inflate method can be found. 
* context - the context you are in. The binding is my activities binding. 
* You can get the root view somehow else.
*/
LayoutTermsBinding termsBinding = LayoutTermsBinding
    .inflate(LayoutInflater.from(context), (ViewGroup) binding.getRoot(), false);

 //without a variable this would be
LayoutTermsBinding termsBinding = DataBindingUtil.
      inflate(LayoutInflater.from(context), R.layout.layout_terms, (ViewGroup) mainBinding.getRoot(), false);

Second step: Set your termsBinding.getRoot() as ContentView:

dialog.setContentView(termsBinding.getRoot());

And you're done. :)

yennsarah
  • 5,467
  • 2
  • 27
  • 48
  • There is a slight change, you might forget it, i have added my answer. – Ravi Jul 20 '16 at 09:25
  • 1
    However your answer is absolutely correct if i have included my view, but here i am using dialog so that will be in another xml file and cannot include that in my main xml. – Ravi Jul 20 '16 at 09:30
  • I have written as a comment in my code, that you'll need to use a variable in your `` tag to use it this way. If you would have posted more information, I could have also provided you your solution. ;) – yennsarah Jul 20 '16 at 09:36
  • yes right, but i have mentioned in my question that i want binding in my dialog to set custom view, so obviously that view cannot be included in xml or i cannot use it in `` – Ravi Jul 20 '16 at 09:37
  • anyways, your answer gave me some hint and i have found solution for that, thanks :) – Ravi Jul 20 '16 at 09:38
  • I meant the `` tag in your `R.layout.layout_terms` - if there is a `variable`, the `inflate` method in `LayoutTermsBinding` will be "provided". – yennsarah Jul 20 '16 at 09:42
  • yes but my question was about how to set custom view in dialog with use of binding, if i will bind that view to dialog then only `` tag inside `R.layout.layout_terms` will be usefull right? – Ravi Jul 20 '16 at 09:43
  • I have added something to my answer for more clarity. Basically, both methods are the same, I like the one with the variable more, because it is less to write (no need to remember the layout id). – yennsarah Jul 20 '16 at 09:50