I have two views, a game view and an admob view.
I want the game view positioned at 0, 0 and with fill parent (so it covers the whole screen), and I want to admob view to be located at 50, 50 (just an example coordinates) and with fill_content.
How do I do this using FrameLayout in Android? Doesn’t matter if its via layout-xml or programatically.
I’ve spent a lot of time searching for examples and tweaking the code myself without any luck.
Specific example code is more appreciate than a link to how views generally work in Android.
Update 1:
This is my current code.
Via XML-layout:
Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/framelayout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
<se.meendo.mapgame.MainMenuView android:id="@+id/mainmenuview" android:layout_width="fill_parent" android:layout_height="fill_parent" />
<com.google.ads.AdView android:id="@+id/ad"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="50dip"
android:layout_marginRight="50dip"
/>
</FrameLayout>
Java:
public class BannerActivity extends Activity {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.banner);
MainMenuView mmv = (MainMenuView) findViewById(R.id.mainmenuview);
if (mmv == null) {
mmv = new MainMenuView(this);
}
AdView ad = (AdView) findViewById(R.id.ad);
if (ad != null) {
ad = new AdView(this, AdSize.BANNER, "a14da0cf5ab5ef7");
AdRequest request = new AdRequest();
request.setTesting(true);
ad.loadAd(request);
}
}
}
Via pure Java
public class BannerActivity extends Activity {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
FrameLayout layout = new FrameLayout(this);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
MainMenuView mmview = new MainMenuView(this);
LayoutParams pl = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.addView(mmview, pl);
AdView adView = new AdView(this, AdSize.BANNER, "****");
LayoutParams pelle = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pelle.setMargins(240, 430, 0, 240);
adView.setLayoutParams(pelle);
// adView.setAdListener(new MyAdListener(this));
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
LayoutParams pl2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
pl2.setMargins(240, 430, 0, 240);
// layout.addView(adView, pl2);
layout.addView(adView);
setContentView(layout);
}
}
Here I trid with coordinates 240, 430 – since I know the banner is 320×50 and I wanted to try and put in the center.
The results
XML-layout: The MainMenu-view showed but not admob-view
Pure Java: The MainMenu-view showed, and the admob-view. But the admob view is shown at 0, 0.
Add just layout_gravity attribute in com.google.ads.AdView view as follow