Adding Admob into ScrollView -> AdBanner disappears
Hey folks
I managed to implement Admob into a normal linear layout so far.
Now I added a additional scrollview
and the adbanner
disappeared. I don´t know what I can do against it.
Follows the code from the .xml where I added the scrollview
:
<?xml versi开发者_高级运维on="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="match_parent">
<ScrollView android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_width="match_parent">
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
[whole bunch of layout elements whoch shouldn´t affect the adbanner]
</LinearLayout>
</ScrollView>
In my linear layout ,where the adbanner still works, the whole adbanner position was done in the main activitiy.java file (did this with help of the tutorial at taiic.com)
// Lookup R.layout.main
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
// Create the adView
// Please replace MY_BANNER_UNIT_ID with your AdMob Publisher ID
String pubID = "xxxxxxxxxxxxxxxxxx";
AdView adView = new AdView(this, AdSize.BANNER, pubID);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
AdRequest request = new AdRequest();
request.setTesting(true);
adView.loadAd(request);
Can anybody tell me what to change or what code to add when implementing an admob banner into a scrollview
?
edit:
i tried to add
<com.admob.android.ads.AdView
android:id="@+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:backgroundColor="#000000"
myapprimaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
android:alignParentBottom="true"/>
between last two lines in the .xml
</LinearLayout>
[here]
</ScrollView>
but then im getting the error "error: Error parsing XML: unbound prefix"
cheers
About the parsing error:
Is this a typo in the question? myapprimaryTextColor="#FFFFFF"
instead of myapp:primaryTextColor="#FFFFFF"
. This would give you the xml parse error.
About the layouts:
Use a RelativeLayout. The working code is at the end of the post. First, some theory :)
Your ScrollView is taking the whole screen, that's why you don't see the admob view. When the scrollview is defined, all the screen is availabel to it, so it takes it. The admob view is actually drawn below your screen. It can be reproduced in this example:
non-working layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:id="@+id/scrollView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test1"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test2"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test3"
/>
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test4"
/>
</LinearLayout>
If you use a RelativeLayout instead, you can set it up so the admob is aligned to the bottom of the screen, and the scrollview above it, taking the rest of the available space.
working layout
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test4"
android:id="@+id/test4"
android:layout_alignParentBottom="true"
/>
<ScrollView android:id="@+id/scrollView1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_above="@id/test4"
>
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test1"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test2"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="300dp"
android:text="Test3"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I used the admob xml version and that is what I use and it works. The ad is at the top. Just copy and paste and you will be scrolling along shortly.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res/com.yourproject.here"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="a14dc1c9d6xxxxx"
ads:adSize="BANNER" />
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
[whole bunch of layout elements whoch shouldn´t affect the adbanner]
</ScrollView>
</LinearLayout>
精彩评论