Reputation: 134
I was trying to add AdMob ads to my application.Ad display on bottom of screen but when it display my last few line hide by Ad.What I can do for automatically up current layout and display Ad. My code is
Window window1 = getWindow();
LinearLayout adsLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.FILL_PARENT);
adsLayout.setGravity(Gravity.BOTTOM);
adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxx");
adsLayout.addView(adView);
AdRequest newAdReq = new AdRequest();
adView.loadAd(newAdReq);
window1.addContentView(adsLayout,lp2);
my xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ScienceActivity"
android:id="@+id/addlayout"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="@string/science" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000"
android:paddingTop="20dp" />
<EditText
android:id="@+id/textbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="10"
android:singleLine="false"
android:editable="false"
android:focusable="false"
android:clickable="false"
android:text="@string/Sciencearticle" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000"
android:paddingTop="20dp"
/>
<Button
android:id="@+id/article"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="5dp"
android:typeface="serif"
android:background="#55ee00"
android:textColor="#ffffff"
android:textSize="25sp"
android:onClick="articleClicked"
android:text="@string/article"
/>
<Button
android:id="@+id/times_article"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:typeface="serif"
android:background="#55ee00"
android:textColor="#ffffff"
android:textSize="25sp"
android:onClick="timesArticleClicked"
android:text="@string/times_article"/>
<LinearLayout
android:id="@+id/addlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:weightSum="1" >
</LinearLayout>
</LinearLayout>
Upvotes: 1
Views: 1701
Reputation: 3332
you can do as follow
XML
<LinearLyaout MAIN
Height:fillparent
Height:fillparent >
<your main content area
Height:fillparent
Height:fillparent
weight:1>
</your main content area>
<Linearlayout
id:+@llAdd
width:fillparent
Height:wrapcontent/>
</LinearLayout>
JAVA
LinearLayout adsLayout = (LinearLayout)findviewbyid(R.id.llAdd);
adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxx");
adsLayout.addView(adView);
AdRequest newAdReq = new AdRequest();
adView.loadAd(newAdReq);
EDITED XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/addlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ScienceActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/addlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="science"
android:textSize="25sp" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000"
android:paddingTop="20dp" />
<EditText
android:id="@+id/textbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:editable="false"
android:ems="10"
android:focusable="false"
android:gravity="top"
android:inputType="textMultiLine"
android:lines="10"
android:singleLine="false"
android:text="Sciencearticle" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000"
android:paddingTop="20dp" />
<Button
android:id="@+id/article"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="#55ee00"
android:gravity="center"
android:onClick="articleClicked"
android:text="article"
android:textColor="#ffffff"
android:textSize="25sp"
android:typeface="serif" />
<Button
android:id="@+id/times_article"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="#55ee00"
android:gravity="center"
android:onClick="timesArticleClicked"
android:text="times_article"
android:textColor="#ffffff"
android:textSize="25sp"
android:typeface="serif" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="@+id/addlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:weightSum="1" >
</LinearLayout>
</LinearLayout>
Upvotes: 2
Reputation: 62391
May this will helps you:
LinearLayout adContainer = <container>;
AdView adView = new AdView(activity, com.google.ads.AdSize.BANNER, <publisher_id>);
// Initiate a generic request to load it with an ad
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(adRequest);
// Place the ad view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
adContainer.addView(adView, params);
Upvotes: 0