I have followed an instruction for making new AdMob plugin for Unity. Ads show up correctly, but I have problem with bottom position. They show up at top of the screen. I have set gravity to bottom (for FrameLayout) but banner ads again show up at the top of screen.
I dont have any .xml file with LinearLayout/FrameLayout tags.
Here is all code:
public class playads {
private String adUnitID = "ca-app-pub-9578188175087140/5483276313";
private Activity activity; //Store the android main activity
private AdView adView; //The AdView we will display to the user
private LinearLayout layout; //The layout the AdView will sit on
public playads () {
activity = UnityPlayer.currentActivity;
activity.runOnUiThread(new Runnable() {
public void run(){
adView = new AdView(activity);
adView.setAdUnitId(adUnitID);
adView.setAdSize(AdSize.BANNER);
AdRequest request = new AdRequest.Builder().build();
adView.setAdListener(new AdListener() {
public void onAdLoaded() {
if(layout == null)
Log.d("null", "null");
{
activity.runOnUiThread(new Runnable() {
public void run(){
layout = new LinearLayout(activity);
layout.addView(adView);
//FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
//p.gravity=Gravity.BOTTOM;
activity.addContentView(layout, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
((FrameLayout.LayoutParams)layout.getLayoutParams()).gravity = Gravity.BOTTOM;
}
});
}
}
}
);
adView.loadAd(request);
}
});
}
}
I really dont know what the problem is. I spent all day to fint it, but without any solution :(
LayoutParams lllp=(LinearLayout. LayoutParams)MyButton. getLayoutParams(); lllp. gravity=Gravity.
android:gravity sets the gravity of the contents (i.e. its subviews) of the View it's used on. android:layout_gravity sets the gravity of the View or Layout relative to its parent.
LayoutParams are used by views to tell their parents how they want to be laid out. See ViewGroup Layout Attributes for a list of all child view attributes that this class supports. The base LayoutParams class just describes how big the view wants to be for both width and height.
Remember that gravity
sets the positioning of a view's children, while layout_gravity
sets the positioning of a view within its parent. So in your case, you want to set the gravity of the LinearLayout, which can be done via member methods. You should also set the orientation.
Your run() method should look something like:
public void run(){
layout = new LinearLayout(activity);
layout.setGravity(Gravity.BOTTOM);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(adView);
LinearLayout.LayoutParams lllp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
activity.addContentView(layout, lllp);
}
Use setGravity in LinearLayout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With