Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DrawerLayout with Admob ad

I am using Navigation-Drawer in my application.So I have created single activity with navigation_drawer layout.I am using fragments to change main content area(@+id/content_frame) data whenever user chooses a menu option from navigation drawer menu.Now my problem is that I want to show admob ad on every screen.I could used fragments to initialize ad layout and inflate it into FrameLayout but I think would be not a good option.Is their any way to initialize ad using that single activty?

navigation_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- The main content view -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/side_navigation_background"
        android:cacheColorHint="#00000000"
        android:choiceMode="singleChoice"
        android:divider="@color/side_navigation_list_divider_color"
        android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>
like image 321
Ansh Avatar asked Jun 17 '13 12:06

Ansh


People also ask

How do you use DrawerLayout?

To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity> . Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.


1 Answers

You can use any layout inside of DrawerLayout. In the .xml shown below you can switch fragments in the single activity (using RelativeLayout) with admob ad in the bottom of this activity.

 <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/relative_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <FrameLayout
                android:id="@+id/fragment"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_above="@+id/adView"
                android:background="@color/background" />

            <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                ads:adSize="SMART_BANNER"
                ads:adUnitId="AD_UNIT_IT"
                ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
                android:gravity="bottom" />
        </RelativeLayout>

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@color/white"
            android:choiceMode="singleChoice"/>
</android.support.v4.widget.DrawerLayout>
like image 148
Yuriy Yunikov Avatar answered Sep 19 '22 06:09

Yuriy Yunikov