6

I have in main.xml TitlePageIndicator and ViewPager, and I want to add admob (right into LinearLayout) at the bottom of RelativeLayout. How can I do this?

When I launch it, the log says nothing about errors (since there is no place to put admob), but admob is invisible, I can`t see it. (looks like admob is outside the screen because I tried to set specific sizes to ViewPager and it works perfect) I do not want to set specific sizes to ViewPager (becouse of the different screen sizes) Thanks.

My main.xml is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.viewpagerindicator.TitlePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"/>   
    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/indicator"/>
    <LinearLayout 
        android:id="@+id/for_ads"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/viewpager"/>
</RelativeLayout>

UPD. solved I used this answer and it works perfect for me

Community
  • 1
  • 1
Zakharov Roman
  • 739
  • 3
  • 13
  • 31

1 Answers1

19

First, your RelativeLayout needs an ID if you want to reference it:

RelativeLayout rLayout = (RelativeLayout)findViewById(R.id.yourRelativeId);

Then create some LayoutParams for the object (in this case, your admob adview) that tell it to align itself to the bottom (and not align to any other views, this way it isn't pushed off-screen or moved by the other views):

 RelativeLayout.LayoutParams rLParams = 
    new RelativeLayout.LayoutParams(
    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    rLParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);

Next, add the view to your RelativeLayout with your LayoutParams:

rLayout.addView(yourAdView, rLParams);
Jose Gonzalez
  • 1,491
  • 3
  • 25
  • 51
Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • 1
    in this case, admob covers up my other UI objects – Zakharov Roman Jun 13 '12 at 16:15
  • Then if you want it to be below another view, just do: rLParams.addRule(RelativeLayout.BELOW, viewtoBeBelow.getId()); But if the view to be below extends to the bottom you won't be able to see the adview, which means instead of the BELOW, you'll have to change the LayoutParams of the view you want to be above the adview with: addRule(RelativeLayout.ABOVE, yourAdView.getId()); And make sure you have an ID set for the adview with setID(someInteger); or it won't be able to place the view relative to it. – Cruceo Jun 13 '12 at 16:34
  • Then you need to make sure you're pointing to the correct ID of the layout in your XML file – Cruceo May 16 '16 at 14:05
  • This solution works fine.But could you tell me , how to dynamically add same view to the bottom of RelativeLayout multiple time one by one in vertical order. – BABU K Jan 27 '17 at 10:22
  • You can either use RelativeLayout.ALIGN_BELOW or RelativeLayout.ALIGN_ABOVE, or a better solution would be to use a vertically-oriented LinearLayout to group the children and then you only have to apply the RelativeLayout params to the group and not the children – Cruceo Jan 27 '17 at 15:49