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>
Related
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());
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 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 a beginner of android, and was studying the layout.
What i am trying to do is i am trying to add a textView to a LinearLayout through code, and the application stops with the error that the application has stopped unexpectedly. The code is:
setContentView(R.layout.main);
LinearLayout abc = (LinearLayout)findViewById(R.id.cLayout);
TextView tv = new TextView(this);
tv.setText("Text Changed!!!!");
abc.addView(tv);
And in the xml, id is set like: android:id="#+id/cLayout".
I know there are better ways to do it, but i want to know why this doesn't work. Contents of textView are changing just fine. Also i have also tried
LinearLayout abc = (LinearLayout)findViewById(R.layout.main);
What am i doing wrong? One thing is clear that i am accessing uncreated items (unassigned pointer).
Edit
Main xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/cLayout"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World"
android:id="#+id/myTV"
/>
</LinearLayout>
I've tried to reproduce it, and it works for me, no exceptions.
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"/>
</LinearLayout>
MyActivity.java:
public class MyActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout ll = (LinearLayout) findViewById(R.id.cLayout);
TextView tv = new TextView(this);
tv.setText("Hello Again!");
ll.addView(tv);
}
}
UPD When I posted your code, I got an exception, too. Make sure you call super.onCreate().
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>