I have tried to change AdMob orientation in Libgdx, when the user reach a screen. I want to put it to the bottom of the screen. I have made a handler, implement it on the appropriate screens, and then call the method, by
myHandler.moveAds(true);
My interface looks like this:
public interface IActivityRequestHandler {
public void showAds(boolean show);
public void moveAds(boolean move);
}
The Android project file looks like this:
public class MyAndroidnewActivity extends AndroidApplication implements IActivityRequestHandler {
protected AdView adView;
RelativeLayout.LayoutParams adParams;
private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;
private final int MOVE_ADS = 1;
private final int PLACE_ADS = 0;
protected Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case SHOW_ADS:
{
adView.setVisibility(View.VISIBLE);
break;
}
case HIDE_ADS:
{
adView.setVisibility(View.GONE);
break;
}
}
}
};
protected Handler moveHandler = new Handler()
{
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case MOVE_ADS:
{
//In my opinion this code is not good, because It doesn't do anything
//when I call the myHandler.moveAds(true); in an another class
adView.setGravity(Gravity.BOTTOM);
break;
}
case PLACE_ADS:
{
adView.setGravity(Gravity.TOP);
break;
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
cfg.useAccelerometer = false;
cfg.useCompass = false;
// Create the layout
RelativeLayout layout = new RelativeLayout(this);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libgdx View
View gameView = initializeForView(new MyApp(this), false);
// Create and setup the AdMob view
adView = new AdView(this, AdSize.BANNER, "MYOWNKEY"); // Put in your secret key here
AdRequest re = new AdRequest();
re.addTestDevice("MYOWNDEVICECODE");
adView.loadAd(re);
// Add the libgdx view
layout.addView(gameView);
// Add the AdMob view
adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
layout.addView(adView, adParams);
// Hook it all up
setContentView(layout);
}
@Override
public void showAds(boolean show) {
handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}
@Override
public void moveAds(boolean move) {
moveHandler.sendEmptyMessage(move ? MOVE_ADS : PLACE_ADS);
}
}
But when I call the myHandler.moveAds(true); method in an another class, it doesn’t do anything, no errors, just the ad on the top of the (landscape) screen, and not on the bottom.
In my opinion there is a problem with my
protected Handler moveHandler = new Handler()
{
@Override
public void handleMessage(Message msg) {
switch(msg.what) {
case MOVE_ADS:
{
//In my opinion this code is not good, because It doesn't do anything
//when I call the myHandler.moveAds(true); in an another class
adView.setGravity(Gravity.BOTTOM);
break;
}
case PLACE_ADS:
{
adView.setGravity(Gravity.TOP);
break;
}
}
}
};
code. Maybe I need to control the orientation in an another way, but I don’t know how. Thanks in advance for any helps!
The
setGravity()method determines where to place the content (in this case the ad) within the AdView, not where to place the AdView inside it’s container.It looks like you’re container is a RelativeLayout, so you should be able to just set new LayoutParams instead of setting the gravity: