Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add view to the bottom of RelativeLayout

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

like image 357
Zakharov Roman Avatar asked Jun 13 '12 15:06

Zakharov Roman


1 Answers

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);
like image 111
Cruceo Avatar answered Sep 24 '22 07:09

Cruceo