Problems adding a new AdMob app to an Android app

I'm trying to add new AdMob ads to my first application, I was able to correctly position the code, but I have problems:

The first problem is that my declaration identifier ca-app-pub-61413779example receives several errors, such as: ca cannot be resolved by a variable, ba strong> cannot be resolved by a variable, literally 61413779 of type int out of range.

The second problem is R.id. mainLayout , which is mainLayout, I do not understand.

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_basic_screen); // Create the adView adView = new AdView(this, AdSize.BANNER, ca-app-pub-61413779example); // Lookup your LinearLayout assuming it been given // the attribute android:id="@+id/mainLayout" LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout); // Add the adView to it layout.addView(adView); adView.loadAd(new AdRequest()); // Initiate a generic request to load it with an ad adView.loadAd(new AdRequest()); 
+4
source share
4 answers

I am not familiar with your add-on, but I have another great way to add admob to your application using only xml.

first in the manifest add these permissions

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/> 

* the second is important

, then add this to your manifest inside the application tag

 <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> 

now in every action you want your ad to appear on you, add this to the layout.xml of the activity.

 <com.google.ads.AdView android:id="@+id/ad" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adUnitId="xxxxxxxxxxxx" ads:adSize="BANNER" ads:loadAdOnCreate="true" android:layout_gravity="center" android:layout_margin="10dp"> </com.google.ads.AdView> 

and inside the top parent layout ex. linearlayout you add them under xmlns: android = "xxx

 xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" xmlns:tools="http://schemas.android.com/tools" 

Advertising should now work fine: D

UPDATE: Remember to download and put the GoogleAdMobAdsSdk-6.4.1.jar file in the libs folder.

+7
source

The AdView constructor expects String to be the third argument, so you are missing double quotes:

 adView = new AdView(this, AdSize.BANNER, "ca-app-pub-61413779example"); 

For your XML, make sure you have the correct ID. If only mainLayout , this is not a valid identifier. If R underlined, it means that your XML has an error somewhere, and R.java is not created.

+1
source

since you customized your content using Activity_basic_screen.xml, you need to go to that xml.

you need to show this code for activity_basic_screen.xml and indicate where you want to place the ad for specific details.

for general, in your activity_basic_screen.xml, you should have LinearLayout or other layouts where you want to show / inflate an ad. for one in your problem with the screenshot / Linearlayout, you should have this one:

  <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/mainLayout" > 

you get this error because i think you don't have a linear layout with

  android:id="@+id/mainLayout" 

you can name it differently

  android:id="@+id/givenName" 

but you have to change your link in java findViewById to "R.id.givenName"

for me, I usually use Ahmed's suggestion, so I don't need to program it in java, but I think that programming in java is more dynamic.

hope this helps.

PS. in your screenshot, you cover the name / project of your application, but you indicated the name of your package. this seems to be the same because you can find your application in a google game using this.

0
source

This may be a different answer for what you are looking for, but I highly recommend using SMART_BANNER on top of BANNER. look at the table that lists the standard banner sizes:

https://developers.google.com/admob/android/banner

Banner: only 320x50 size; Smart Banner: resize to screen width x 32 | 50 | 90

Here is an example of how to do this:

-Layout

  <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main_r" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.adexample"> <RelativeLayout android:id="@+id/adView_test" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> </android.support.constraint.ConstraintLayout> 

-Activity:

  public class MainActivity extends AppCompatActivity { private AdView adView; private AdRequest adRequest; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MobileAds.initialize(this, "YOUR_ADMOB_APP_ID"); admobCall(); } // Function to Set Ads for Main Layout private void admobCall(){ // Set the RelativeLayout id to the view View adContainer = findViewById(R.id.adView_test); adView = new AdView(this); adView.setAdSize(AdSize.SMART_BANNER); //Real Admob Main Activity Banner adView.setAdUnitId(getString(R"YOUR_ADMOB_AD_ID")); // Test google ID for Banner // adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); ((RelativeLayout)adContainer).addView(adView); // This Request will bulid the ADs as a Test //adRequest = new AdRequest.Builder() //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build(); // This Request Will bulid the ADs as Real adRequest = new AdRequest.Builder().build(); adView.loadAd(adRequest); } 

Also, be sure to add google () to the project level build.gradle, and this implementation depends

 implementation 'com.google.android.gms:play-services-ads:17.2.1' 

Visit the site with implementation instructions to make sure you don’t miss something:

https://developers.google.com/admob/android/quick-start

Hope this was helpful :)

0
source

Source: https://habr.com/ru/post/1499704/


All Articles