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!
Related
I am trying to find a solution to my problem by an hour and I found some topics with my similar problem, but I don't understand how to resolve.
I was developing an app with Processing for Android and I wanted to try AdMob (just for learning, for now), so I followed instructions all over the net and now I am stuck.
To add ads in the app, I was following Google instructions to add the SDK of Play Service and to add banners, but I am stuck here:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
initPdService();
// Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pu....83399");
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);
AdRequest adRequest = new AdRequest.Builder()
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
The problem is that it give me "linearLayout cannot be resolved or it is not a field", and I don't know how to resolve. I think that the problem is in findViewById(R.id.linearLayout), since the error is at "linearLayout" and not "LinearLayout".
I am sure that a lot of people find this problem, so I think that is a common problem. I already added import android.widget.LinearLayout;at the top of the sketch, so I don't know what is the problem.
Other infos: it is targeting Android SDK 19, as I know that Play Services want at least API level 13.
EDIT:
Here is my [old] activity_main.xml found is /res/layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v7.widget.Space
android:id="#+id/space1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/adView"
android:layout_marginTop="208dp"
android:layout_toRightOf="#+id/adView" />
<View
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
EDIT 2:
This is my new activity_main.xml, with linearLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
<android.support.v7.widget.Space
android:id="#+id/space1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/adView"
android:layout_marginTop="208dp"
android:layout_toRightOf="#+id/adView" />
<View
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</LinearLayout>
</RelativeLayout>
You need to set your content view:
setContentView(R.layout.activity_main);
which you have commented out. Do you have an xml file "activity_main.xml" that contains the LinearLayout with an id, linearLayout?
If yout don't have a LinearLayout with the id linearLayout, you need to add one. Otherwise, it's trying to find a view (by id!) that doesn't exist. For example:
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
...
</LinearLayout>
Your issue is that you are using Processing as a library on top of the Android SDK, and it doesn't have access to the layout files, as in Processing you create your screens programatically - it is similar to one big Canvas class for those that are familiar with Android and not Processing.
What the actual compilation error is saying is that the generated R.java file is not finding the compressed resources
I've never tried to add AdMob, but really i think your issue here is that you need to instantiate a LinearLayout programatically, not via XML - as again, you won't have access to that through the PDE.
EDIT:
just noticed that you are using Eclipse (and LibPd !!)... ok, so you can reach a layout
Can you show the code where you are importing Processing into the Activity ?
I created a menuView.xml layout to be in all of the layouts of my activity. This layout has one column on each border and a title bar like this:
ComposeView http://img845.imageshack.us/img845/2121/d6zp.png
I insert this layout in the other layouts this way:
<!-- Show menu -->
<com.example.MenuView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
But if one of the layouts has full screen view, part of this view gets covered by the MenuView, so...
How could I tell to this view to adapt its size to the blank space inside the MenuView to not get covered by it?
UPDATE -- full XML included
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/degradado">
<!-- Show menu -->
<com.example.MenuView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:id="#+id/Left_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" >
//Here go buttons, views, etc...
</RelativeLayout>
<RelativeLayout
android:id="#+id/Right_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" >
//Here go buttons, views, etc...
</RelativeLayout>
What happens here is that these 2 Relative layouts get covered by the MenuView (The darkest gre borders and the top black bar), and the ideal way would be that these 2 layouts get fitted to the blank space (the clearest gray).
I can solve this setting margin sizes to the Relative layouts to fit inside of it, but i know this is not the best way to do it, so I don't know if there is another way.
I think the best way to solve your issue is with inheritance.
If you define an Activity that can be used as a template for all your fleshed out Activitys to add their content to.
I don't know what you custom menu is 'made of' but as a simple example:
Create a basic Activity with code:
public class ActivityWithMenu extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_with_menu_layout);
}
}
and 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"
tools:context=".ActivityWithMenu" >
<TextView
android:layout_width="match_parent"
android:layout_height="20dip"
android:background="#ff000000"
android:textColor="#ffffffff"
android:text="Main Menu Title Bar"
android:id="#+id/mainmenutitle" />
<LinearLayout
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_below="#+id/mainmenutitle"
android:layout_alignParentLeft="true"
android:background="#ff999999"
android:orientation="vertical"
android:id="#+id/lefthandmenu" />
<LinearLayout
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_below="#+id/mainmenutitle"
android:layout_alignParentRight="true"
android:background="#ff999999"
android:orientation="vertical"
android:id="#+id/righthandmenu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/righthandmenu"
android:layout_toRightOf="#+id/lefthandmenu"
android:layout_below="#+id/mainmenutitle"
android:orientation="vertical"
android:id="#+id/activitycontent"
/>
</RelativeLayout>
Then create your xml for a specific Activity, in this case a simple 'Hello World' layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00ff00"
android:text="Hello World!"
/>
</LinearLayout>
</ScrollView>
But now when you write the code for this Activity, extend 'ActivityWithMenu' instead of the Activity class direct and inflate this xml layout as follows:
public class Activity1 extends ActivityWithMenu
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
LinearLayout ll = (LinearLayout)findViewById(R.id.activitycontent);
ScrollView sv = (ScrollView)this.getLayoutInflater().inflate(R.layout.activity1_layout, ll, false);
ll.addView(sv);
}
}
I have added the code for making the Activity fullscreen here instead of in the parent ActivityWithMenu class assuming that you wouldn't want them all displayed that way but you could move it into the parent class if appropriate.
Hope this helps.
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.
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>
I manage to add adMob to the title screen, which layout is written in a *.xml-file.
setContentView(R.layout.main);
AdView adView = (AdView)findViewById(R.id.ad);
adView.requestFreshAd();
main.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/de.xazen.tictactoe"
android:background="#drawable/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
myapp:backgroundColor="#000000"
myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
/>
</RelativeLayout>
The game itself is written in Tttview.
public class TttView extends View
The Activity Game.java create a instance of TttView and uses setcontent(tttView) to use it as layout.
public class Game extends Activity{
private TttView tttView;
.
.
.
setContentView(tttView);
}
How can I add adMob to the game itself?
Well it's not obvious from your question what tttView is or how it is setup, but if it creates a layout type to display you can add a AdView programatically into a LinearLayout or similar like this:
// Setup layout parameters
LinearLayout.LayoutParams params;
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// Create a linear layout
LinearLayout layout = new LinearLayout(mContext);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setPadding(6,6,6,6);
AdView mAdView = new AdView(this);
layout.addView(mAdView,params);
Or if you are loading the layout from an XML file you can do similar what you have above
setContentView(R.layout.game);
Where game.xml is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/de.xazen.tictactoe"
android:background="#drawable/title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<tttView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
...
/>
<com.admob.android.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
myapp:backgroundColor="#000000"
myapp:primaryTextColor="#FFFFFF"
myapp:secondaryTextColor="#CCCCCC"
/>
</RelativeLayout>