1

This may be a dumb question, so my apologies if so; I'm fairly new to Android.

But anyway - I have a working ViewStub, which is replaced by different layouts depending different situations. It's working fine with regards to showing the correct layout when I call the setLayoutResource() method, and then setVisibility to VISIBLE. However, I now need some of the content in this view that is being shows to be dynamic (i.e. I need to set it via code rather than just show a static layout).

Is this even possible? The setLayoutResource() method only takes a static layout-resource ID, but I need that layout XML file to be able to have it's TextViews contain non-static text that comes from some code that I have ready to utilize. How should this be approached if possible? I understand the concept of having a Java class, and inflating the XML to attach itself to it to update the fields, but I can't see how that relates to my code at hand, since it's simply a layout resource int I need to set for the setLayoutResource() method in ViewStub.

I can post existing code if needed, but I'm not sure it do much more than clutter up the post. For reference - All I have is a simple layout XML file with some TextViews, and then my main XML containing the ViewStub, which is part of a custom Dialog. The user is able to instantiate the Dialog and set the layout, which in turn sets the layout of the ViewStub. This is the layout in which I need the dynamic content to be used.

Any advice would be greatly appreciated. Thanks!

svguerin3
  • 2,433
  • 3
  • 29
  • 53

1 Answers1

2

Turns out this wasn't too difficult to accomplish. I just needed to use the ID of the TextView layouts after inflating the ViewStub to get a copy of the actual TextViews, then I was easily able to set their text to whatever kind of dynamic/custom text I desired.

I also needed to comment out the code that shows it via the .VISIBLE call, and instead do the following (the .inflate() line of code accomplishes the same thing as setting it to VISIBLE):

 View inflatedView = dialog.myStubView.inflate();
 TextView myTextView = (TextView) inflatedView.findViewById(R.id.my_text_view);
 myTextView.setText("Dynamic/Custom Text");
svguerin3
  • 2,433
  • 3
  • 29
  • 53
  • 1
    if you want to use `ViewStub` with data/view binding, see the answer here: https://stackoverflow.com/a/53439022/3763032 alternative approach by using `ViewStubProxy` – mochadwi Mar 08 '20 at 03:21