Reputation: 2534
Been banging my head against the wall about this for a while. Trying to get an AdMob AdView to show up above a ScrollView and have said ScrollView fill up the remainder of the screen. So far all I've been able to do is get the AdView to show up and load the test ad properly, but then the rest of the screen is just black. I can't figure out where the hell the ScrollView's gone.
I've tried a dozen different solutions, including loading it programattically instead of via main.xml and changing the height of the ScrollView to wrap_content, but still having the same issue. Also tried setting the height of the ScrollView to 0px and the weight to 1 per another thread's suggestion, but that doesn't work either. I'm guessing this is a simple answer but I'm just stumped right now.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainLayout"
>
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
ads:adUnitId="[my ad id]"
ads:adSize="BANNER"
/>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawSelectorOnTop="false"
android:background="[background drawable]">
[snip all the crap within the scrollview which is irrelevant]
</ScrollView>
</LinearLayout>
and then in my main .java file's oncreate call:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//programattically create adview
//AdView adView = new AdView(this, AdSize.BANNER, "[my ad id]");
//find main layout and add adview to it
//LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
//layout.addView(adView);
//xml adview
AdView adView = (AdView)this.findViewById(R.id.adView);
//set up ad request with test devices/emulator
AdRequest request = new AdRequest();
request.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(request);
Anyone have any suggestions?
Upvotes: 1
Views: 1863
Reputation: 5229
Your initial LinearLayout
should specify android:orientation="vertical"
, and then make the AdView
height wrap_content
instead (or 50dp - this is what I have in one of my apps). This should be sufficient. The ScrollView
is correct as you have it, with wrap_content
and android:layout_weight="1"
.
Upvotes: 1
Reputation: 2534
Okay, found a way to make this work. Got kind of maddening there for a while.
The method I found that worked was switching to a RelativeLayout, loading the ad programmatically and throwing in a ViewStub that inflates a view the same height as the ad when the ad loads (using AdListener). I suspect there's a more elegant way to do this, but this definitely works for me.
main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ViewStub
android:id="@+id/mainPlaceholder"
android:inflatedId="@+id/mainAdInflated"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout="@layout/ad_mob_layout"
/>
<ScrollView
android:layout_below="@+id/mainAdInflated"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="[background drawable]"
>
[snip irrelevant views]
</ScrollView>
</RelativeLayout>
ad_mob_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<View
android:layout_width="fill_parent"
android:layout_height="50dip"
/>
</LinearLayout>
[main].java:
public class [main] extends Activity implements AdListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//programattically create adview
AdView adView = new AdView(this, AdSize.BANNER, "a14e0010a66c749");
//find main layout and add adview to it
RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);
layout.addView(adView);
//set up ad request with test devices/emulator
AdRequest request = new AdRequest();
request.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(request);
adView.setAdListener(this);
}
@Override
public void onReceiveAd(Ad arg0) {
@SuppressWarnings("unused")
View stubToInflate = ((ViewStub)this.findViewById(R.id.mainPlaceholder)).inflate();
}
Hope this helps if anyone else is having this problem!
Upvotes: 0
Reputation: 36302
You made the AdView
height fill_parent
. Make it wrap_content
instead and the height of the ScrollView
to be fill_parent
.
Upvotes: 0