Jayesh Prajapati
Jayesh Prajapati

Reputation: 19

insert admob in android fragment?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:gravity="bottom" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:textAlignment="gravity" />

    <LinearLayout
        android:id="@+id/ll_ad"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-5456867664363319/8478541984" >
        </com.google.android.gms.ads.AdView>
    </LinearLayout>

</RelativeLayout>

package htm.greensoftware.com;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;

public class HomeFragment<ChildFragment> extends Fragment {

    WebView wv;
    private AdView adView;
    private AdRequest adRequest;

    public HomeFragment() {
    }

    @SuppressLint("NewApi")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container,
                false);

        wv = (WebView) rootView.findViewById(R.id.webView1);
        wv.loadUrl("file:///android_asset/index.html");
        adView = new AdView(getActivity());

        adView.setAdUnitId("ca-app-pub-5456867664363319/8478541984");
        adView.setAdSize(AdSize.BANNER);
        LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.ll_ad);
        layout.addView(adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        adView.loadAd(adRequest);
        return rootView;

    }

}

I googled but no proper not able to got answer. i tried following links. AdMob in a fragment Using AdMob from within a Fragment? Android Admob ad not shown in Activity with fragment. Why?

Upvotes: 1

Views: 2189

Answers (1)

William
William

Reputation: 20196

It is not clear what your question is, BUT

  1. Yes, you can show AdView in a Fragment
  2. yes, your config looks approximately correct.
  3. You have marked your WebView as android:layout_height="fill_parent" so it will grab all the screen height giving none to your AdView. If you want I to expand to fill unused space then your will need to use a containing LinearLayout and use layout_weight="1" in order to indicate it should expand.

Upvotes: 1

Related Questions