how to add admob banner above ListView (ListFragment)?

527 views
Skip to first unread message

Fujitora

unread,
Aug 9, 2014, 5:22:16 AM8/9/14
to google-adm...@googlegroups.com
before I learned about fragment, I was using ListView and admob on XML layout file.

but now I'm using ListFragment. I don't know how to add admob banner above my listview because I didn't use any xml file. 

here's my code to show admob. but the admob banner show in the middle of the screen not on top of the screen.

    // HomeFragment.java
    public class HomeFragment extends ListFragment {
    private AdView adView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getActivity().setTitle(R.string.app_name);
        mMainCategories = new ArrayList<String>();
        mMainCategories.add("Fruit");
        mMainCategories.add("Vegetable");
        mMainCategories.add("Animal");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, mMainCategories);
        setListAdapter(adapter);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, savedInstanceState);
        adView = new AdView(getActivity());
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId("xxxxxxxxx");
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("xxxxxxxxxxxxxxxxxxxx") 
                .build();
        adView.loadAd(adRequest);
        container.addView(adView);
        return v;
    }
    }

here's the hosting activity for HomeFragment

    // HomeActivity.java
    public class HomeActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
        FragmentManager fm = getFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
        if(fragment == null) {
            fragment = new HomeFragment();
            fm.beginTransaction().add(R.id.fragmentContainer, fragment).commit();
        }
    }
    }

and this is the layout where I put the fragment

    // activity_fragment.xml
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragmentContainer"
        android:layout_width="match_parent" android:layout_height="match_parent" />

I was also trying to add admob on activity_fragment.xml. the admob banner show on top of the screen but it show in front of my listview.

sorry for my english. thank you very much.

Amy Quispe (AdMob SDK Team)

unread,
Aug 11, 2014, 8:18:22 PM8/11/14
to google-adm...@googlegroups.com
Hi Fujitora,

Just to make sure - are you trying to put the Banner above the ListFragment or in the ListFragment?

Amy
Reply all
Reply to author
Forward
0 new messages