Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set gravity for LinearLayout programmatically

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 :(

like image 679
Andrew Avatar asked Aug 09 '14 21:08

Andrew


People also ask

How to set gravity of LinearLayout programmatically in Android?

LayoutParams lllp=(LinearLayout. LayoutParams)MyButton. getLayoutParams(); lllp. gravity=Gravity.

What is the usage of the command Android layout_ 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.

What is LayoutParams in Android?

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.


2 Answers

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);

    }
like image 53
scottt Avatar answered Oct 26 '22 14:10

scottt


Use setGravity in LinearLayout

like image 23
user1406716 Avatar answered Oct 26 '22 14:10

user1406716