adding other content in top and bottom of MySurfaceView - android

I am designing an android application contain MySurfaceView includes a Canvas to draw images in a thread running through the application,
(layout is default main.xml which create with project)
but i have to add a bar/textView in the top and add an AD in the botton of the screen.
then how can i do this ?
my declaration is like this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mySurfaceView = new MySurfaceView(this);
setContentView(mySurfaceView);
}
May be my given information is not enough to understand the situation, so if you find any obscurity in my question please ask me
Thanks in advance

Hmm, one possibility is that you could just put it inside of a RelativeLayout or something similar? This assumes that you don't want to just define it in the xml which would probably look cleaner. The code would probably look something like this (you can definitely make it more efficient/just use a LinearLayout, I just threw down the first thing that came into my head):
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams paramsTop =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add the textView to the top
paramsTop.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
TextView textView = new TextView(this);
textView.setText("Hello World");
// Give them id's since we need to position relative to one another
textView.setId(1);
layout.addView(textView, paramsTop);
AdView adView = new AdView(this, AdSize.BANNER, INSERT_ADMOB_ID_HERE);
adView.setId(2);
RelativeLayout.LayoutParams paramsBottom =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add the adView to the bottom
paramsBottom.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layout.addView(adView, paramsBottom);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(adRequest);
SurfaceView view = new SurfaceView( this );
view.setId(3);
RelativeLayout.LayoutParams paramsMiddle =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add the surfaceView in between the textView and the adView
paramsMiddle.addRule(RelativeLayout.BELOW, textView.getId());
paramsMiddle.addRule(RelativeLayout.ABOVE, adView.getId());
layout.addView(view, paramsMiddle);
setContentView(layout);

Related

How to move burstly ads to the center of the screen in android?

The MainActivity is extending UnityPlayerActivity.
We are using the following code snippet to place banner ads on android.
FrameLayout layout1 = new FrameLayout(this);
BurstlyAnimatedBanner levelUpBanner = new BurstlyAnimatedBanner( this, layout1, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT), "0455184989016204562", "Level_Up_Screen", 30, false);
LayoutParams lp1 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
addContentView(layout1,lp1);
Doing this is showing the ads at the top-left corner of the screen. I need the ads at the top-center of the screen. I tried using layout1.setForegroundGravity(Gravity.CENTER); but that did not help. How can I achieve what I need? Also, I need to do this programatically and cannot use and xml.
I'm the developer of the Burstly Unity plugin at github.com/Burstly/BurstlyUnityPlugin.
You can center the banner ad by adding it to a RelativeLayout instead of a FrameLayout as we do in the plugin:
mBaseLayout = new RelativeLayout(mActivity);
mBaseLayout.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
mActivity.addContentView(mBaseLayout, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.topMargin = (int)originY;
lp.leftMargin = (int)originX;
burstlyView.setLayoutParams(lp);
mBaseLayout.addView(burstlyView);
With this, you can then position the banner at will. If you wish to position it at the top center, then you'd want to change lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT) to lp.addRule(RelativeLayout.CENTER_HORIZONTAL).

Put Items in RelativeLayout dynamically

I have done some research, but the answer i found does not work for me. Here is some part of my code. the R.id.relative is the id of the relativelayout in the xml file
RelativeLayout RL = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView title = new TextView(this);
title.setText(" History ");
title.setId(99099);
title.setTextSize(30);
title.setGravity(Gravity.CENTER);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
title.setLayoutParams(params);
RL.addView(title);
RelativeLayout.LayoutParams test_params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test = new Button(this);
test.setText(" Back ");
test_params.addRule(RelativeLayout.BELOW,99099);
test.setId(199291);
test.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params);
RL.addView(test);
RelativeLayout.LayoutParams test_params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test2 = new Button(this);
test2.setText(" Clear ");
test_params2.addRule(RelativeLayout.BELOW,test.getId());
test2.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params2);
RL.addView(test2);
all 3 items did show up, but they stack together. I can't get them below another.
Could anyone help ?
From what I've been able to find out, you have to add the view using LayoutParams. Here's an example:
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
parentView.addView(linearLayout, relativeParams);
And to relatively position your items programmatically you have to assign ids to them, this stops them from 'overlapping'.
TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);
Then addRule(RelativeLayout.RIGHT_OF, tv1.getId());
Change test.setLayoutParams(test_params2); to test2.setLayoutParams(test_params2);
Explanation: You have inadvertently set the layout params of test a second time, with params that instruct it to be below itself, which I guess just puts it top-left (default placement in a RelativeLayout). Since you never give test2 any layout params, it also gets default placement. So everything is at the top and thus appear atop each other.
Incidentally, if you just want them arranged linearly, why not use a vertical LinearLayout?
The rule you have added is causing your view to be stacked together.If you need to add it should be used by the following rule.
test_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
Next, add the view to your RelativeLayout with your LayoutParams:
RL.addView(yourAdView, rLParams);
same goes for the each cases.try to run.It will solve your problem

show admob ads at the bottom of the RelativeLayout using java no XML

I have designed a game with no xml layouts.I have used my own view extending the SurfaceView class .This is the code i am using.
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
_ad=new AdView(this,AdSize.BANNER,"################");
rl.addView(myView);
rl.addView(_ad);
_ad.loadAd(new AdRequest());
setContentView(rl);
myView is the instance of class extending SurfaceView
and
_ad is the AdView instance.
I want to show the the ads at the bottom of the screen.But they are getting displayed only at the top.
Please suggest me what to do.I will be very grateful.
The only mistake I was doing was not associating my LayoutParams lay variable with the _ad AdView instance.So here is the code after modification.
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
_ad=new AdView(this,AdSize.BANNER,"################");
rl.addView(myView);
rl.addView(_ad,lay); //line which was changed.Everything else was correct
_ad.loadAd(new AdRequest());
setContentView(rl);
Thank You StackOverflow.

Center MoPub baner on SurfaceView

I'd like add MoPub ad in my Android game. Game is created on SurfaceView Class. I have no xml layout file. The problem is that I can't center ad on screen. I try center and gravity everything. Still doesn't work. MoPubView extends FrameLayout.
MoPubView mAdView = new MoPubView(this);
mAdView.setAdUnitId("_MY__ID_");
mAdView.loadAd();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout mRelativeLayout = new RelativeLayout(this);
mRelativeLayout.setGravity(Gravity.CENTER_HORIZONTAL);
mRelativeLayout.addView(mSurface);
mRelativeLayout.addView(mAdView, layoutParams);
mRelativeLayout.invalidate();
setContentView(mRelativeLayout);
Please advise me something because it drawing me crazy. I feel I'm missing something easy.
You can't center the content directly in MoPub, or at least I haven't found a way to do so. It's based on FrameLayout.
You have to center the ad in HTML. If you are using custom HTML code inside your MoPub campaign, just wrap the code inside <div style="text-align:center">...</div>. (Do this through the MoPub administration web). It solved my problem, I was using an OpenX ad provider.
Just create a container layout with "FILL_PARENT" parameters and add your ad view into it like this:
MoPubView mAdView = new MoPubView(this);
mAdView.setAdUnitId(XXXXXXX);
RelativeLayout adContainer = new RelativeLayout(this);
addContentView(adContainer, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
LayoutParams adParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1); //to align vertically at top
adParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1); //to align horizontally at center
adContainer.addView(mAdView, adParams);
mAdView.loadAd();
I know it is not good idea to set constant banner width but as long as your banners are 320px it works fine:
final float scale = getResources().getDisplayMetrics().density;
MoPubView mAdView = new MoPubView(this);
mAdView.setAdUnitId(MOPUB_ID);
mAdView.loadAd();
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams((int) (320*scale), LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
RelativeLayout mRelativeLayout = new RelativeLayout(this);
mRelativeLayout.addView(mSurface);
mRelativeLayout.addView(mAdView, layoutParams);
mRelativeLayout.invalidate();
setContentView(mRelativeLayout);
I have done same thing with below code snippet.I am using LibGDX java game engine.
Relativelayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
moPubView = new MoPubView(mActivity.getApplicationContext());
moPubView.setAdUnitId("XXXXXX");// Banner test Ad unit Id.
moPubView.setAutorefreshEnabled(true);
moPubView.setVisibility(View.GONE);
layout.addView(moPubView,adParams);
1.I have Relative layout in parent.
2.Added MoPubView in the parent Relative layout with layoutParams only ALIGN_PARENT_BOTTOM && CENTER_HORIZONTAL
layout.addView(moPubView,adParams);

Admob on surfaceview

I have an activity which has a surfaceview capturing whole screen. i want to put an admob ad on surfaceview. I found this link but it does not work. Any idea ?
In your activity's onCreate:
// Add admob ads.
admobView = new AdView(this, AdSize.BANNER, "YOUR_UNIT_ID");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
admobView.setLayoutParams(lp);
RelativeLayout layout = new RelativeLayout(this);
layout.addView(surfaceView);
layout.addView(admobView);
admobView.loadAd(new AdRequest());
setContentView(layout);
IN the example above, "surfaceView" is your surfaceView instance, that you should instantiate like you do today.
Don't forget to call admobView.destroy() in your activity's onDestroy.

Categories

Resources