Sid
Sid

Reputation: 582

Alignment of Ads in Scrollview in Android

I am trying to create a page on Android with Ads (using ad mob). Following code I am using for it:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/adView_relative_layout" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Primary Screen sssssssss ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss sssssssssssssssssssssssssssssssss ssssssssssssssssssssssssssssssssssssssssss sssssssssssssssssssssssssssssssssssssssss ssssssssssssss s jkjkljlkjkljsjkjlkjskjkjkl jkljkljkl jkljlkj kljkljkl jkljlkjkljkl jkljlkjkljlkjlk jlkjlkjlkjlkjlkjlkj jkljlkjlkjlkjlkjlkjlkjlkjlk jlkjlkjlkjkljlkjlkj lkjlkjlkjklj kljlkjlkjlkjkljlkjlkjlkjlkjlkjlk lkjlkjlkjlkjlkj jlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkj lkjlkjlk lkjlkjlknigpoihgpoj k lkjcfhkl jiuvlk jkjhu hiuhjlkjliijv kjgjhhgkjjhlkkjk"
                android:textSize="20sp" />

            <Button
                android:id="@+id/SwitchToSecond"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Switch to Second Screen"
                android:textSize="20sp" />
        </LinearLayout>
    </ScrollView>

    <RelativeLayout
        android:id="@+id/adView_relative_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" >

        <com.google.ads.AdView
            android:id="@+id/adView"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="@string/MY_AD_UNIT_ID"
            ads:loadAdOnCreate="true" />
    </RelativeLayout>

</RelativeLayout>

But the problem is the ads are coming at top and bottom of the screen (that's what I want) but It's covering the text at the top and bottom of the text by ad sections (Even I scroll up or down).And I am unable to see texts

Please guide how to align texts and scroll view so that I can see all the text along with the ad view.

Find below my Activity.java code:

package com.abc.testapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.view.*;
import android.content.Intent;
import com.google.ads.*;

public class HomeActivity extends Activity {
    /** Called when the activity is first created. */
    Button screen2Button=null;
    //private AdView adView;
     private AdView adView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_home);
        screen2Button=(Button)findViewById(R.id.SwitchToSecond);
        screen2Button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
            Intent myIntent=new Intent(v.getContext(),SecondScreen.class);
            startActivity(myIntent);
            }
        });
        String s1 = getResources().getString(R.string.MY_AD_UNIT_ID);
     // Create the adView
        adView = new AdView(this, AdSize.BANNER, s1);

        // Lookup your LinearLayout assuming it's been given
        // the attribute android:id="@+id/mainLayout"
        RelativeLayout layout = (RelativeLayout)findViewById(R.id.LinearLayout1);

        // Add the adView to it
        layout.addView(adView);

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_home, menu);
        return true;
    }

    @Override
    public void onDestroy() {
      if (adView != null) {
        adView.destroy();
      }
      super.onDestroy();
    }

}

Upvotes: 1

Views: 3365

Answers (1)

mattgmg1990
mattgmg1990

Reputation: 5876

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:orientation="vertical" >

<com.google.ads.AdView
    android:id="@+id/adView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/MY_AD_UNIT_ID"
    ads:loadAdOnCreate="true" />

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/adView1"
    android:layout_above="@+id/adView2" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Primary Screen sssssssss ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss sssssssssssssssssssssssssssssssss ssssssssssssssssssssssssssssssssssssssssss sssssssssssssssssssssssssssssssssssssssss ssssssssssssss s jkjkljlkjkljsjkjlkjskjkjkl jkljkljkl jkljlkj kljkljkl jkljlkjkljkl jkljlkjkljlkjlk jlkjlkjlkjlkjlkjlkj jkljlkjlkjlkjlkjlkjlkjlkjlk jlkjlkjlkjkljlkjlkj lkjlkjlkjklj kljlkjlkjlkjkljlkjlkjlkjlkjlkjlk lkjlkjlkjlkjlkj jlkjlkjlkjlkjlkjlkjlkjlkjlkjlkjlkj lkjlkjlk lkjlkjlknigpoihgpoj k lkjcfhkl jiuvlk jkjhu hiuhjlkjliijv kjgjhhgkjjhlkkjk"
            android:textSize="20sp" />

        <Button
            android:id="@+id/SwitchToSecond"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Switch to Second Screen"
            android:textSize="20sp" />
    </LinearLayout>
</ScrollView>


<com.google.ads.AdView
    android:id="@+id/adView2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/MY_AD_UNIT_ID"
    ads:loadAdOnCreate="true" />

Edit: I think you should try just using a linear layout since you want something very linear (ad, then ScrollView, then ad). I have edited the xml above to reflect that. It is untested though, so you might need to tweak it a bit.

Edit 3: Added a second AdView above the ScrollView to meet the requirement of one AdView at the top and one AdView at the bottom. Changed back to a RelativeLayout so that one AdView can be aligned at the top and one aligned at the bottom.

Upvotes: 1

Related Questions