0

I am having loads of problems with Banner Ads, namely hiding them if the consumer makes any type of purchase in my App.

These problems can be found here:

setVisibility(View.GONE) causes a Crash

So basically I have a new question.

I have a class MainActivity.java with the associated XML. It is possible to have two options of the XML layout (one with the ad, one without), and through a bit of java code assign one of these two XML layouts to the MainActivity class at runtime?

If so, how can it be done?

Saveen
  • 4,120
  • 14
  • 38
  • 41
Rewind
  • 2,554
  • 3
  • 30
  • 56
  • Did you try view.INVISIBLE (I believe... plz check docs). the problem with GONE is that it removes the View while INVISIBLE hides it. -Kf – K F Aug 11 '18 at 22:58
  • Yes, I did thanks. That was one of the answers. Unfortunately it did not work. – Rewind Aug 11 '18 at 23:08

2 Answers2

1

There are lots of possible solutions. Here's the simplest one:

Step #1: Create the two layouts — here, I'll call them R.layout.with_ads and R.layout.without_ads

Step #2: When you call setContentView() in onCreate() of your activity, pass in either R.layout.with_ads or R.layout.without_ads, based on whatever criteria you wish to use to decide which to use

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Can you do this if the view was already set in the activity? Or would you have to call `onCreate()`? – K F Aug 11 '18 at 22:59
  • @kfrajer: You can't call `onCreate()`. You can always call `setContentView()` again, though there are less disruptive solutions. – CommonsWare Aug 11 '18 at 23:02
  • @CommonWare I believe the OP is asking to change the view dynamically. – K F Aug 11 '18 at 23:13
  • @kfrajer: I do not see that in "It is possible to have two options of the xml layout (one with the ad, one without), and through a bit of java code assign one of these two xml layouts to the MainActivity class at runtime?" – CommonsWare Aug 11 '18 at 23:14
  • @CommonWare This is the opening of the OP: `having loads of problems with Banner Ads, namely hiding them if the consumer makes any type of purchase in my App`. the consumer made a purchase... change the layout, so I understand. Hence, my question. I let the OP answer or clarify if needed. – K F Aug 11 '18 at 23:30
  • This is the answer, but I think I will not go with this as it will mean I have to have the webpage loaded twice. Also, when I change views everything in the javascript memory of one webview will not be in the other. So it is defeating the object. However, this answer is the correct one for the question I asked. – Rewind Aug 12 '18 at 21:36
0

Yes, you can use conditionals such as if statements or a switch, for instance:

if (your requirement) -> setContentView(layout A)

else -> setContentView(layout B)

R. drt
  • 26
  • 4