Having 2 Possible XML layouts for the same java Class
Clash Royale CLAN TAG#URR8PPP
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?
Yes, I did thanks. That was one of the answers. Unfortunately it did not work.
– Rewind
Aug 11 at 23:08
2 Answers
2
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
R.layout.with_ads
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
setContentView()
onCreate()
R.layout.with_ads
R.layout.without_ads
Can you do this if the view was already set in the activity? Or would you have to call
onCreate()
?– kfrajer
Aug 11 at 22:59
onCreate()
@kfrajer: You can't call
onCreate()
. You can always call setContentView()
again, though there are less disruptive solutions.– CommonsWare
Aug 11 at 23:02
onCreate()
setContentView()
@CommonWare I believe the OP is asking to change the view dynamically.
– kfrajer
Aug 11 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 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.– kfrajer
Aug 11 at 23:30
having loads of problems with Banner Ads, namely hiding them if the consumer makes any type of purchase in my App
Yes, you can use conditionals such as if statements or a switch, for instance:
if (your requirement) -> setContentView(layout A)
else -> setContentView(layout B)
Thank for the answer
– Rewind
Aug 12 at 21:34
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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
– kfrajer
Aug 11 at 22:58