I need to implement AdMob's AdView in the bottom of the Activity with the ListView. I added AbView to my layout sucessfully. My layout file looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/archiveLayout"
android:background="#FFFFFF"
>
<ListView
android:id="#android:id/list"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_above="#+id/adView"
android:background="#000000"
android:layout_height="wrap_content"
/>
<TextView android:id="#android:id/empty"
android:gravity="center|center"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="You havn't received any quote yet!"
/>
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="320dip"
android:layout_height="50dip"
ads:adUnitId="ID"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_alignParentBottom="true"/>
and that is how I add it in my activity's onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.archive_list);
setListAdapter(new QuoteAdapter(this,
android.R.layout.simple_list_item_1, reverse(getContentDatabase(this))));
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.setEnabled(true);
adView = new AdView(this, AdSize.BANNER, "ID");
AdRequest re = new AdRequest();
re.addTestDevice(AdRequest.TEST_EMULATOR);
re.addTestDevice("E83D20734F72FB3108F104ABC0FFC738");
adView.loadAd(re);
}
it is shows properly with defined width and height in the bottom of the screen. But there's no content in it. Just the black empty screen. Tell me please what I can do with it.
I found it much easier to just add it solely in the xml file. It looks like you've blended the two ways together. You've got ads:loadOnCreate enabled, and you're also making a new one programmatically. Try doing it just the XML way, it loads up when the page is brought up, and I've had 100% fill rate using the xml only method. No need to complicate things if you're just trying to display it when the xml adview shows.
The problem was that I didn't receive ads:adUnitId. When I did it with the admob site everything works properly.
Related
I am integrating AdMob Banner ad in my Activity. The layout consist of a GridView and AdView.
What I Want:-
When ad is not shown, i.e. at start or when ad is getting downloaded or anything, I want the GridView to have all the screen. But when ad is to be displayed, it should cover required space below but do not overlap anything.
My Layout:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
android:id="#+id/ll_gridmain"
tools:context="mohits.app.picreddit.itemgrid">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:divider="#null"
android:numColumns="2"
android:background="#454545" >
</GridView>
<LinearLayout android:id="#+id/banner_view"
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:visibility="gone">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_banner"
android:visibility="gone">
</com.google.android.gms.ads.AdView>
</LinearLayout>
</LinearLayout>
My Activity:-
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("xxx")
.build();
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
mAdView.setVisibility(View.VISIBLE);
bannerView.setVisibility(View.VISIBLE);
}
});
What I am Getting:-
The banner_view layout is always showing even if I am setting it to GONE. This should only become visible when the ad is to be shown.
Thank you.
The problem is even if you remove your banner, the weight of the GridView will still be 9 and the weightSum of the layout is 10, so the GridView will not fill the whole LinearLayout.
Removing the following line from your layout should work:
android:weightSum="10"
Also, setting the visibility of the AdView is redundant.
Setting the visibility of a ViewGroup will also affect their children.
You could remove the following attribute from your AdView:
android:visibility="gone"
After that, setting the visibility of banner_view should be enough:
#Override
public void onAdLoaded() {
super.onAdLoaded();
bannerView.setVisibility(View.VISIBLE);
}
I have a PreferenceActivity for a LiveWallpaper and have used the solution here (LINK) to create an Admob ad. It works, but the ad disappers when the user scrolls up or down and loads again when it comes into view. Is there a way so that the ad always stays at the bottom (or top) of the activity (like in a regular activity)?
Before
((LinearLayout)view).addView(adView);
Try setting the layoutParam like
adView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT))
OR Change the layout like following:
<?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:id="#+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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="#string/ad_unit_id"/>
</RelativeLayout>
And code part would be like this:
mAdView = (AdView) findViewById(R.id.adView);
mAdView.setAdListener(new ToastAdListener(this));
mAdView.loadAd(new AdRequest.Builder().build());
Android 2.3.3
I have followed the steps, found in the this tutorial. When I run the application in my device(mobile), the ads are getting displayed. No Problem here. But when I try to view the activity in my eclipse, I get a blank screen with the NullPointerException. I have taken the screenshot. Please refer to it.
Then, I have clicked on the link, given in the exception's "detail" (SourceFile:670) and I get the following details..
I have clicked on the Add Source, and browsed to the jar file in my application->libs folder. Then I get the following...
Here are my xml and java codes that I have used...
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="#000000"
android:orientation="vertical" >
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="a151xxxxxxxxx"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"/>
<TextView
android:id="#+id/txtViewPrevious"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:gravity="right"
android:height="25dp"
android:singleLine="true"
android:text=""
android:textSize="16sp" />
.....
.....
Java
private AdView adView;
adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest();
adView.loadAd(adRequest);
Libs
How do i get rid of these exceptions?
Thanks in Advance...
This happens. It's normal. Eclipse can't display custom views sometimes (most of the time actually).
Download the latest SDK. Per the release notes, this was fixed in v6.3.0.
I have problem with AdMob in my Android application. I have done exactly everything described here, but when I start the program, it doesn't run (there are a lot of bugs). I will show you a fragment of program:
public class MenuMainActivity extends Activity{
private AdView adView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_main);
adView = new AdView(this, AdSize.BANNER, "(code-15 signs)");
RelativeLayout layout = (RelativeLayout)findViewById(R.layout.activity_menu_main);
layout.addView(adView);
adView.loadAd(new AdRequest());
And also xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/titleOfGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:text="#string/title_of_game_text" />
...
When I comments two lines:
//layout.addView(adView);
//adView.loadAd(new AdRequest());
Everything is ok. Where is the problem?
If you use findViewById() you must pass an id, there you are passing a "layout".
should be something like:
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adLayer);
create a Layout with "adLayer" as the "id" and position it in your layout where you like the Ads to show up.
As the previous posters have mentioned you need to create a specific place in your layout where you want yout advert to appear, for example, using your supplied code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/titleOfGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:text="#string/title_of_game_text"/>
<RelativeLayout
android:id="#+id/adLayer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
etc. etc./>
You then need to change the code in your activity to :
RelativeLayout layout = (RelativeLayout)findViewById(R.id.adLayer);
as BrainCrash suggested.
Hope this helps!
I am new to android development, but I am trying to add admob to my app which is using a surfaceview (panelView - GameView).
I tried following http://rx-games.com/admob-adverts-on-surfaceview-no-xml-tutorial/ but I must be doing something wrong, as rl.addView(panelView); adds a nullPointerException. Any help is greatly appreciated.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//setContentView(R.layout.main);
AdView adView = new AdView(this, AdSize.BANNER, "a14ded47ad3779e");
panelView = (GameView) findViewById(R.id.gameScreen);
RelativeLayout rl = new RelativeLayout(this);
rl.addView(panelView);
rl.addView(adView);
setContentView(rl);
threadView = panelView.getThread();
}
You are inflating the GameView class from an xml layout and add it to a root View that is not the one defined in that same xml. You have two options:
You create your SurfaceView entirely in Java (as in the example you posted)
You include the AdView in the xml layout (I would prefer this option over the other one)
If you name the xml file game_layout.xml, you use it as:
setContentView(R.layout.game_layout.xml);
game_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="#string/admob_id"
ads:adSize="BANNER"
ads:loadAdOnCreate="true"
android:layout_alignParentBotom="true"
/>
<your.package.GameView
android:id="#+id/gameScreen"
android:layout_alignParentTop="true"
android:layout_above="#id/adView"
/>
</RelativeLayout>