Search code examples
androidadmobfragmentandroid-listfragment

Admob integration with AppCompatActivity


I am struggling to add admob to my application which is based on AppCompatActivity. I have tried several ways like autogenerated way (admob) some how not succeed. Below is the main activity class for reference.

 public class WorldClockActivity extends AppCompatActivity {
    private static final boolean IS_GINGERBREAD = Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD;

      /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    private GoogleApiClient client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager fm = getSupportFragmentManager();
        // Create the list fragment and add it as our sole content.
        if (fm.findFragmentById(android.R.id.content) == null) {
            ClockListFragment list = new ClockListFragment();
            fm.beginTransaction().add(android.R.id.content, list).commit();
        }
        //try to add any other view
MobileAds.initialize(this,
                "ca-app-pub-xxxxxxxxxxxxxxxx~xxxxxxxxxx");

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxx/xxxxxxxxxx");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

Where as other fragment ClockListFragment activity code is given below.

public static class ClockListFragment extends ListFragment implements
        LoaderManager.LoaderCallbacks<Cursor>, PauseSource {

    private CursorAdapter mAdapter;
    private ActionMode mMode;
    private OnSharedPreferenceChangeListener mSpChange;
    private boolean mAutoSortClocks;
    private final List<PauseListener> mListeners = new ArrayList<>();

Not sure I am doing silly mistake. Any help would be appreciated.


Solution

  • I am able to fix it by replacing with android.R.id.content with a base layout

    setContentView(R.layout.base_layout_to_bedeleted);
        MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxx");
        mAdView = (AdView)findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    
        FragmentManager fm = getSupportFragmentManager();
        // Create the list fragment and add it as our sole content.
        //if (fm.findFragmentById(android.R.id.content) == null) {
            ClockListFragment list = new ClockListFragment();
            //fm.beginTransaction().add(android.R.id.content, list).commit();
            fm.beginTransaction().add(R.id.content_pane, list).commit();