Dynamically add a view to the end of a RelativeLayout

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

When I run it, the log says nothing about errors (since there is no place to add admob), but admob is invisible, I can not see it. (it looks like admob is off screen because I tried to set specific sizes in ViewPager and it works fine) I don't want to set specific sizes in ViewPager (due to different screen sizes) Thanks.

My main.xml:

<?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 great for me

+6
source share
1 answer

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

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

Then create some LayoutParams objects for the object (in this case, your admob adview) that tell it to align to the bottom (and not be tied to any other views, so it doesn't get squeezed out of the screen or not other types of moves):

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

Then add the view to your RelativeLayout using LayoutParams:

 rLayout.addView(yourAdView, rLParams); 
+18
source

Source: https://habr.com/ru/post/917989/


All Articles