<?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"
tools:context="com.shakthi.preg_zlite.MainActivity">
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#FFFFFF"
android:gravity="center" >
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/adview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="ADUNIT"
ads:adsSize="BANNER"
/>
</RelativeLayout>
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:stretchMode="columnWidth"
android:layout_marginTop="50dp"
android:numColumns="2"
android:horizontalSpacing="15dp"
android:layout_height="match_parent"
android:padding="25dp"
>
</GridView>
</RelativeLayout>
JAVA:
package com.shakthi.preg_zlite;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import com.google.android.gms.ads.AdActivity;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 0:
Intent ppp = new Intent();
ppp.setClass(MainActivity.this, Pregnancyplanner.class);
startActivity(ppp);
break;
case 1:
Intent cc = new Intent();
cc.setClass(MainActivity.this, Duedatecalc.class);
startActivity(cc);
break;
case 2:
break;
case 3:
break;
default:
break;
}
}
String[] title = { "Pregnancy planner","Due date calculator","Basic pregnancy info","Reminder" };
int[] icon={R.mipmap.prepregnancyplanner,R.mipmap.duedatecalc,R.mipmap.laboursign,R.mipmap.reminder};
GridView grid;
@Override
protected void onCreate(Bundle savedInstanceState) {
String AD_UNIT_ID = "XXXXX";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adView = (AdView) this.findViewById(R.id.adview1);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.loadAd(new AdRequest.Builder().build());
grid = (GridView) findViewById(R.id.grid);
CustomGrid adapter=new CustomGrid();
grid.setAdapter(adapter);
grid.setOnItemClickListener(this);
}
class CustomGrid extends BaseAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
Viewholder holder = null;
if (convertView == null) {
holder = new Viewholder();
convertView = inflater.inflate(R.layout.custom_list, parent,
false);
holder.name = (TextView) convertView.findViewById(R.id.title);
holder.image = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (Viewholder) convertView.getTag();
}
holder.name.setText(title[position]);
holder.image.setImageResource(icon[position]);
return convertView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
class Viewholder {
ImageView image;
TextView name;
}
}
}APP level gradle:apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.shakthi.preg_zlite"
minSdkVersion 19
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.google.android.gms:play-services-ads:9.4.0'
// compile files('libs/GoogleAdMobAdsSdk-6.3.0.jar')
}Project level gradle:// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I was using 4.0.4 with no issues and then updated on Admob to 4.1.0.
I have the adviews set up in the layout XML (my ad unit is changed
obviously and I know it's valid because it's in a live app that is
serving ads with no problems) :
<com.google.ads.AdView android:id="@+id/topad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="1234"
ads:adSize="BANNER"/>
And the Adview is defined in attrs.xml as:
<declare-styleable name="com.google.ads.AdView">
<attr name="adSize">
<enum name="BANNER" value="1" />
<enum name="IAB_MRECT" value="2" />
<enum name="IAB_BANNER" value="3" />
<enum name="IAB_LEADERBOARD" value="4" />
</attr>
<attr name="adUnitId" format="string" />
</declare-styleable>
Logcat doesn't give me much:
05-11 23:14:03.182: WARN/Ads(17648): Invalid unknown request error:
Cannot determine request type. Is your ad unit id correct?
05-11 23:14:03.185: INFO/Ads(17648): onFailedToReceiveAd(Invalid
Google Ad request.)
05-11 23:20:52.138: ERROR/Ads(17800): AdView missing required XML
attribute "adSize".
05-11 23:20:52.205: ERROR/Ads(17800): AdView missing required XML
attribute "adSize".
1-i finished making my project i tried every way to put admob and
build app and try it on my phone and it doesn't show the ad can any one help me .
2-and where to add the banner is it in the tabbed activity only or in
the fragments and in the other activities that contain pdf viewer and web view ??
this is java file .
public class tap extends AppCompatActivity {
private static final String TAG = "tap";
private SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tap);
Log.d(TAG, "onCreate : starting");
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
// Set up the ViewPager with the section adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tablayout = (TabLayout) findViewById(R.id.tabs);
tablayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new pharmacist(),"Ph");
adapter.addFragment(new dentist(),"Dent");
adapter.addFragment(new doctor(),"MD");
adapter.addFragment(new pook(),"on");
viewPager.setAdapter(adapter);
}
}
this is the layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:ads="http://schemas.android.com/apk/res-auto"
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_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.testpharmacist.tap">
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="50dp"
android:layout_height="25dp"
android:layout_marginTop="10dp"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>