Rewind
Rewind

Reputation: 2814

Having 2 Possible XML layouts for the same java Class

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?

Upvotes: 0

Views: 56

Answers (2)

R. drt
R. drt

Reputation: 26

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

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

else -> setContentView(layout B)

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006604

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

Upvotes: 1

Related Questions