i am trying to add Ads to my current activity but the activity is not showing any Ad . i have also add internet permission to Android Manifest.xml
Here is my
.Gradle File
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "sms.and.call.alert.flashlight.lpa.com.myapplication"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:9.6.0'
compile 'com.google.firebase:firebase-ads:9.6.0'
}
This is MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(getApplicationContext(), "ca-app-pub-3940256099942544~3347511713");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest request = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
// An example device ID
.build();
mAdView.loadAd(request);
}
}
This is MainActivity.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/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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="sms.and.call.alert.flashlight.lpa.com.myapplication.MainActivity">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
Admob-Firebase initial configuration guide
updated 10/04/2016 - MM/dd/yyyy
Step 1 : Be sure to have the latest version of Google Play Services and Google Repository
Step 2 : Put the google-services.json in the app folder after you had created the project on Firebase console
More Info:https://firebase.google.com/docs/android/setup#add_firebase_to_your_app
Step 3 : Set up your project level build.gradle as below. More Info: https://firebase.google.com/docs/android/setup#add_the_sdk
buildscript {
// ...
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
// ...
classpath 'com.google.gms:google-services:3.0.0'
}
}
Step 4 : Setup your app level build.gradle as below: More info: https://firebase.google.com/docs/admob/android/quick-start#integrate_firebase_and_the_mobile_ads_sdk
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "your.package.app"
minSdkVersion 9
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.google.firebase:firebase-ads:9.0.0'
}
apply plugin: 'com.google.gms.google-services'
Step 5 : Add those permissions to the AndroidManifest if they are not present yet
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Moreover be sure that your package name in the AndroidManifest match the applicationId of you app build.gradle or you will get a compilation error.
Step 6 : Continue to follow the Quick-Start tutorial and learn how to create Ad-Units to show ads on your views: https://firebase.google.com/docs/admob/android/quick-start
Step 7 : If you still have compile time problems, check those really usefull and common-case threads and examples:
Manifest and applicationId problems: https://stackoverflow.com/a/37177173/2910520,
Multiple flavors problems:
https://stackoverflow.com/a/34990999/2910520
Google Admob examples:https://github.com/googleads/googleads-mobile-android-examples
Maybe you forgot to load the Ad by calling:
AdView mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(request);
Related
I'm following the kotlin fundamentals code lab and in it it says that anything under API 21 will convert vector images to png images and to prevent that I need to add xmlns:app="http://schemas.android.com/apk/res-auto" to the activity_main.xml file and press Sync Now, and add vectorDrawables.useSupportLibrary = true to the defaultConfig { } in the .gradle(:app) file. I followed everything but I kept getting an error:
/Users/Home/IdeaProjects/DiceRoller/app/src/main/res/layout/activity_main.xml:12
error: attribute android:srcCompat not found.
error: failed linking file resources.
I realized that the xmlns:app="http://schemas.android.com/apk/res-auto" line is grayed out
This is the gradle version I'm using:
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-alpha18
What is causing this problem?
activity_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical"
tools:context=".MainActivity">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/diceImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:srcCompat="#drawable/empty_dice"
tools:src="#drawable/dice_1">
</android.support.v7.widget.AppCompatImageView>
build:grade(:app)
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.diceroller"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.core:core-ktx:1.2.0-beta02'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}
build.gradle:
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0-alpha18'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
This is because the srcCompat attribute only exists in the app (http://schemas.android.com/apk/res-auto) namespace for the AppCompatImageView class (see the API documentation for more info), not the default android (http://schemas.android.com/apk/res/android) namespace.
To fix this, simply rename the android portion of android:srcCompat to app:srcCompat:
<android.support.v7.widget.AppCompatImageView
app:srcCompat="#drawable/empty_dice" />
(P.S. Consider self-closing the XML tag such that you don't have to write more code to close the XML element. See this question for more info)
I have developed an app that use AdMob for showing ads. The problem is that test ads are showing instead of real ads in API 24 and above, while work properly in API 23 and below.
I do not know which is the problem but I can assure that :
1. I do not implement .addTestDevice
2. I use my my apps unitID and not the test ID
I attach my onCreate method :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_edit_text_note);
//Δημιουργία διαφημίσεων banner
//Φόρτωση διαφημίσεων banner
mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
and my layout file is :
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:contentDescription="#string/advertisement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-4565************/27********">
</com.google.android.gms.ads.AdView>
my build.gradle :
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion '28.0.3'
defaultConfig {
applicationId "com.sebastian_pa.sebastian_v2"
minSdkVersion 16
targetSdkVersion 27
versionCode 4
versionName "1.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:27.0.1'
implementation 'com.android.support:design:27.0.1'
testImplementation 'junit:junit:4.12'
implementation 'com.google.android.gms:play-services-ads:17.0.0'
}
Does anyone knows why this is happening?
There are 3 places that you need to change from your test id to your own id.
In manifest file
In you initialize code
In your xml file
From your post, I can see you only did the last one.
For step 1&2, have a look here https://developers.google.com/admob/android/quick-start?hl=en-US#import_the_mobile_ads_sdk
For step 3 have a look here https://developers.google.com/admob/android/banner?hl=en-US
I have followed the sample google video as well as 5 other relevant "How to make a banner" videos and no one is running into my problem. When I add the following code to my xml (within relative layout)
com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitID="ca-app-pub-9079489398627366/3701601134">
</com.google.android.gms.ads.AdView>
It asks me to add xmlns:ads="http://schemas.android.com/apk/res-auto
I do then everything looks fine. Once I compile I get the error:
Error:(72) No resource identifier found for attribute 'adUnitID' in package 'mypackage'
Here is what I have done with other necessary files:
MANIFEST
<uses-permission android:name="android.permission.INTERNET" />
<uses- permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent"></activity>
My java code:
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
My gradle:
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "willvanbuskirk.com.trumpfactsandquotes"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.firebase:firebase-core:9.6.0'
compile 'com.google.firebase:firebase-ads:9.6.0'
}
apply plugin: 'com.google.gms.google-services'
did you forgot initialization by using your app_id, not ad_unit_id
MobileAds.initialize(getApplicationContext(),getString(R.string.adMob_app_id));
I am getting a build error as soon as I enable dataBinding for my library project:
AAPT: No resource type specified (at 'text' with value '#{user.name}')
If I enable dataBinding for the application module, it works fine.
But if I enable dataBinding for my lib project, I get the above error.
build.gradle of application module
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
dataBinding{
enabled true
}
defaultConfig {
applicationId "xyz.databindingtrial"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
compile project(path: ':librarytrial')
}
build.gradle of lib project
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
dataBinding{
enabled true
}
defaultConfig {
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.3.0'
}
The layout file:
<data class="UserTrackingBinding">
<variable
name="user"
type="xyz.databindingtrial.model.User"/>
</data>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
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="xyz.databindingtrial.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{user.name}"/>
</RelativeLayout>
The activity code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UserTrackingBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User("Test");
binding.setUser(user);
}
User Model
public class User extends BaseObservable {
private final String name;
public User(String name){
this.name = name;
}
#Bindable
public String getName() {
return name;
}
}
Does dataBinding not work with library projects? If it does, what is wrong with my setup?
Thanks
It does work with library projects but any app that depends on a library that uses data binding has to enable data binding even if they don't use it.
Figured out the issue. The package structure messed up things for me. The package structure in the manifest and the actual package structure was different. Reason being we are dealing with legacy codebase and it got overlooked. It is working fine.
Im following the steps in the android docs here https://developer.android.com/training/implementing-navigation/lateral.html#horizontal-paging
I use Android studio 1.2.1.1
As soon as i add their code to my XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
i get
-Rendering problems the following classes could not be found:
android.support.v4.view.ViewPager (Fix Build Path, Create Class)
Tip: Try to build the project.
Ive tried:
Switching between API 21 and 22
Trying Holo Themes
Adding support libraries 4 and 13
heres gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "ian.marxbrothers"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.android.support:support-v4:22.0.0'
compile 'com.android.support:support-v13:22.0.0'
}
There actually is no answer. The Viewpager will not show anything but you can still adjust its size within a layout and then add its contents in xml