Showing ads on Android

edited February 2014 in Android Mode

I made a game for android, and now I want to display ads on it. The problem is the guys at Google AdSense don't want to accept me into their programm, so I can't use AdMob (there's a tutorial for using admob with android, so that's a shame).

I tried to find some alternatives to AdMob -like StartApp, Chartboost, etc- but I can't get them working. Mostly because I need to add some code to the android layout files (main.xml) and it seems processing doesn't get along well with this.

Has anybody succeded with this? Do you know how to get ads working?

Thank you very much.

Answers

  • If AdMob isn't an option, then I don't think there are going to be any tutorials out there. However, if you post what you have tried (with the main.xml file, etc.), we / I may be able troubleshoot what is causing the problem. In the worst case scenario, you can move your project into Eclipse (easy with Processing's export) and continue to develop natively while still using the Processing core libraries.

  • edited February 2014

    Oh, thanks a lot n.n

    I am already working on eclipse, and tried mixed some native code. It went like this:

    What I did was follow the StartApp tutorial. I tried to add a normal banner, so what I had to do was add a library -that I put in the libs folder of my exported android project-, then updated the manifest file with the required permissions and activities (StartApp requires you add two activities).

    Then, I added this code at the beggining of my program (before calling setup):

    (at)Override
    public void onCreate(){
          StartAppAd.init(this, "<Your Developer Id>", "<Your App ID>");
    }
    

    Replacing of course my developer ID and App ID.

    Up to here I had no problems and my app worked perfectly -but without ads, of course-.

    Then came the problems.

    The StartApp documentation PDF said that to add a banner to my app, I had to put the following code in my layout file -main.xml-:

    <com.startapp.android.publish.banner.bannerstandard.BannerStandard
    android:id="@+id/startAppStandardBanner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>
    

    I put it but nothing happened. I've read that processing works in its own activity or something, so I thought "Well, let's add the banner by code".

    StartApp also offers a way to do this, with this code:

    // Get the Main relative layout of the entire activity
    RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.mainLayout);   
    // Define StartApp Banner
    Banner startAppBanner = new Banner(this);
    RelativeLayout.LayoutParams bannerParameters =
    new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT,
    RelativeLayout.LayoutParams.WRAP_CONTENT);
    bannerParameters.addRule(RelativeLayout.CENTER_HORIZONTAL);
    bannerParameters.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);    
    // Add to main Layout
    mainLayout.addView(startAppBanner, bannerParameters);
    

    I tried puting it in different places of my code, first I tried in the onCreate, then in the setup, then in the draw.

    It worked fine in the onCreate, except for the RelativeLayout.LayoutParams bannerParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); line, that got me a "Can't create handler inside thread that has not called Looper.prepare()" error.

    If I commented out that line everything worked fine again, but -of course- no adds showed up.

    Thank you very much for trying to help me :)

    Edit: My main.xml is the base one that comes with every exported android processing project. It just has a linear layout. I tried changing it for a relative layout that filled the screen, but that didn't change anything at all.

  • You're very close. Keep in mind that I haven't used the StartApp API myself - but if you describe it correctly, you should be able to display the ad banner with either the XML or the Java approach.

    If you want to go the XML route, can you provide the main.xml file that you're working with? It's probably something simple in your activity's attributes, or, perhaps, something to do with the placement of the banner tag within the layout...

    If you would prefer the Java way, then you are even closer. You are correct in placing the code snippet in onCreate(). The only problem is that you have to call the code from the UI thread. I'm not entirely sure why it isn't the UI thread to begin with... but this is trivial to fix:

    //Android's native function for running code on the UI thread
    runOnUiThread(new Runnable() {
      @ Override
      public void run() {
        //Code to run on UI thread:
    
        // Get the Main relative layout of the entire activity
        RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.mainLayout);   
        // Define StartApp Banner
        Banner startAppBanner = new Banner(this);
        RelativeLayout.LayoutParams bannerParameters =
        new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
        bannerParameters.addRule(RelativeLayout.CENTER_HORIZONTAL);
        bannerParameters.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);    
        // Add to main Layout
        mainLayout.addView(startAppBanner, bannerParameters);
      }
    });
    
  • Sorry, I have a question; where and how should I put that in my code?

    I tried this:

    public void myThread(){
        Thread th=new Thread(){
    
             @Override
             public void run(){
                  try
                  {
                       while(true)
                       {
                       Thread.sleep(100);
    
    
                        AutoLudum.this.runOnUiThread(
                                new Runnable() {
                                    @Override
                                    public void run() {
                                          //Code to run on UI thread:
    
                                          // Get the Main relative layout of the entire activity
                                          RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.mainLayout);  
                                          // Define StartApp Banner
                                          Banner startAppBanner = new Banner(this);
                                          RelativeLayout.LayoutParams bannerParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                                          bannerParameters.addRule(RelativeLayout.CENTER_HORIZONTAL);
                                          bannerParameters.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);   
                                          // Add to main Layout
                                          mainLayout.addView(startAppBanner, bannerParameters);
                                    }
                                }
                            );
    
                       }
                  }catch (InterruptedException e) {
                // TODO: handle exception
               }
             }
        };
        th.start();
    }
    

    and just pasting in inside my onCreate, but in both cases I get the run() in public void run() { underlined with this markers "The method run() of type new Runnable(){} must override a superclass method" and "implements java.lang.Runnable.run".

    I also get the "new Banner(this);" underlined with "The constructor Banner(new Runnable(){}) is undefined".

    What should I do?

    Sorry, I don't know much about java, and thanks a lot for your previous answer.

  • edited February 2014

    Please answer my previous mensaje, but I think I've made that work. Now another problem arose.

    RelativeLayout mainLayout = (RelativeLayout)findViewById(R.id.mainLayout); is getting my mainLayout from main.xml, yes. But processing isn't using a layout declared by me. In fact, I never called setContentView, so I have no controll over what layout I'm using, and because of that I'm getting a null pointer exception when the code reaches the mainLayout.addView(startAppBanner, bannerParameters); part.

    Do you have any ideas of what should I do? Maybe you know a way to replace mainLayout.addView with something like Parent.addView, but I'm not sure how this works...

  • The code snippet from your previous message seems incredibly over-complicated, but if it works, then I'll leave it...

    You should be able to call setContentView() passing your layout file (R.layout.main). This must be the first call in onCreate() (immediately after super.onCreate()). However, I'm not sure how Processing will handle this...

    Can you post your entire code (or at least onCreate() and the main.xml file)? I'm afraid that I don't entirely understand what's going on here. Your error messages suggest that something is inherently wrong with the way that you are trying to do things...

  • Hi, I tried integrating admob into a processing sketch on similar lines.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        LinearLayout parent = (LinearLayout) findViewById(R.id.LinearLayout2);
        AdView ad = new AdView(this, AdSize.BANNER, "my id");
        parent.addView(ad);
        AdRequest r = new AdRequest();
        r.setTesting(true);
        ad.loadAd(r);
    }
    

    This was entered, and used the linear layout in main.xml. Now this shows ads but the processing sketch is gone. On the contrary, if i remove this snippet completely, the sketch works fine. So how can i add an adView to the processing sketch?

    Thanks

Sign In or Register to comment.