I'm trying to put an AdMob banner into my app but half the time it doesn't show any content.
The AdListener even receives an call to onAdLoaded and I never get any error.
I noticed that if the ad isn't showing and I put the app in the background somehow the ad starts to show. For example if I:
close and reopen the app
start an fullscreen interstitial ad
start the in-app purchase overlay
All these actions trigger an update that somehow redraws/remeasures the views.
I tried many different things to simulate this in onAdLoaded.
Is it possible that AdMob can't find a matching ad even if I don't get any errors? According to the website the match rate is 98%. The number of impressions is about a third of the requests.
I found more posts with similar issues (all from ~7 years ago) but none of their answers worked in my case:
Admob not showing up until phone is locked
AdMob won't show the banner until refresh or sign in to google plus
Android AdMob: addView doesn't show ad until return to activity
Admob ads appear only after first refresh
I tried to get this to work for over two days. Please take a look at my code, I annotated it with explainations:
MainActivity:
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
if(privacyPolicyAcceptedAndHasNoPremium()){ //at first i check the shared preferences
binding.adPlaceholder.setVisibility(View.VISIBLE); //the adPlaceholder starts as GONE when i set it to VISIBLE the rest of the content slides up
List<String> testDevices = Arrays.asList(AdRequest.DEVICE_ID_EMULATOR, "...");
RequestConfiguration requestConfiguration = new RequestConfiguration.Builder().setTestDeviceIds(testDevices).build();
MobileAds.setRequestConfiguration(requestConfiguration);
MobileAds.initialize(MainActivity.this, initializationStatus -> {});
adView = new AdView(this); //i create the adView programmatically but using the xml view element causes the same problem
adView.setAdSize(AdSize.LARGE_BANNER);
adView.setAdUnitId("...");
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() { // this gets called so i think the ad is successfully downloaded from the google server. i put all kinds of code in here to force the adView to show
//setContentView(binding.getRoot());
//binding.adPlaceholder.addView(new View(MainActivity.this));
//binding.adPlaceholder.forceLayout();
//binding.constraintLayout.requestLayout();
//adView.invalidate();
//adView.bringToFront();
//adView.setVisibility(AdView.GONE);
//adView.setVisibility(AdView.VISIBLE);
//adView.setBackgroundColor(Color.TRANSPARENT);
}
}
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
binding.adPlaceholder.addView(adView); //i add the adView into the empty LinearLayout. i tried to add it directly to binding.constraintLayout too.
adView.loadAd(adRequestBuilder.build());
}
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
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">
<!-- this is the empty container into which i load the ads. it sits at the bottom of the screen and is not visible before an ad is showing -->
<LinearLayout
android:id="#+id/adPlaceholder"
android:layout_width="match_parent"
android:layout_height="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:orientation="vertical"
android:visibility="gone" />
<!-- this MotionLayout contains the rest of the app (except the toolbar). it slides up when an ad is showing -->
<androidx.constraintlayout.motion.widget.MotionLayout
android:id="#+id/motionLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layoutDescription="#xml/activity_main_scene"
app:layout_constraintBottom_toTopOf="#+id/adPlaceholder"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".MainActivity">
...
</androidx.constraintlayout.motion.widget.MotionLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application ...>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="..." />
...
</application>
Edit:
I finally found a way that works for me.
I'm just setting the window background color to the color that it already has. This is really fast and doesn't recreate the activity.
However I don't know why this works and why only this worked for me. I guess it updates the views in a similar way to the many other solutions from the other questions.
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
getWindow().setBackgroundDrawableResource(R.color.background);
}
}
Related
I have converted my wordpress blog into app using Webview. But now i am facing serious issue of the time my android webview takes to update blog images on my app.
Whenever i change image in an existing blog, i have checked it on browser it takes nearly 2-3 seconds for updating the image. But my android webview shows me the old image for 4-5 minutes even after wordpress website is updated with new image.
I have tried clearing webview cache and also tried allowing dom storage, but it didn't helped me either. I have also used a refresh button to reload the page in webview, but still it shows me old image for 3-4 minutes.
So can anyone please help me to reduce this time issue that I am facing currently. I have added my code of the webview below
.Java file
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fantasy__football);
this.setTitle("Football");
F_foo = findViewById(R.id.fantasyf_Adview);
AdRequest f_adRequest15 = new AdRequest.Builder().build();
F_foo.loadAd(f_adRequest15);
F_web = (WebView)findViewById(R.id.fantasy_f_web);
WebSettings webSettings = F_web.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(false);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setDomStorageEnabled(true);
F_web.clearCache(true);
F_web.loadUrl("muyrl.com");
F_web.setWebViewClient(new WebViewClient());
F_web.setWebViewClient(new WebViewClient(){
#Override
public void onPageFinished(WebView view, String url){
super.onPageFinished(view,url);
view.clearCache(true);
}
});
Button ref = (Button)findViewById(R.id.ref_fb);
ref.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
F_web.clearCache(true);
F_web.reload();
}
});
.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
tools:context=".Fantasy_Football">
<WebView
android:id="#+id/fantasy_f_web"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/fantasyf_Adview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxx"
tools:layout_editor_absoluteX="369dp"
tools:layout_editor_absoluteY="446dp" />
<Button
android:id="#+id/ref_fb"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:text="Refresh"
/>
</RelativeLayout>
So can anyone please help me with this issue or suggest me what i can do to update my images faster ?
I am actually stuck in this error for the last 24h and still can't find solution
the error description:
11-06 18:53:37.745: E/AndroidRuntime(1698): Caused by:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)'
on a null object reference
this is some of the xml layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_pattern"
android:orientation="vertical" >
.
.
.
.
.
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-xxxxxxx/xxxxxxxx" />
<TextView
android:id="#+id/tv_05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
oncreate method in mainActivity.java :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adview = (AdView) this.findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("")
.build();
adview.loadAd(adRequest);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(mainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(AppConstants.Interstitial_Id);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
.....
i can't see any error in this i checked all imports libraries everything is ok
Please help me I'm running out of time :/
It is failing because it is not finding an element with id adView in your layout.
Make sure the XML file you displayed above is actually activity_main.xml.
And do a clean build.
in XML replace this
xmlns:ads="http://schemas.android.com/apk/res-auto"
instead of
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
I faced Similar issue. When i checked closely in resourse file There were two resourse file for my activity:
\res\layout\activity_main_lauch.xml
\res\layout-v21\activity_main_lauch.xml
I was modifing single file, hence it was throwing error.
when i apply the change in both files it started working.
Hope it might help.
You need to put this into your com.google.android.gms.ads.AdView and in your LinearLayout :
xmlns:ads="http://schemas.android.com/apk/res-auto"
EDIT:
You need to pull ID number of your test device from LogCat. Just use the AdManager method and set it to TEST_EMULATOR like the logcat says. If you run on an actual device with usb debugging and watch the logcat, the ID will appear in there.
I have an app that for some reason, on the latest Android SDK (4.4) launches to a black screen.
I can still hear the activity behind it (for example if I click on somewhere in the black, a button sound is clicked, so Its almost like the screen is loaded behind the black screen).
It works fine on my device which is a Nexus 4 running 4.4, it appears to be an issue on the later version (Nexus 5) devices.
It also shows the banner ad (admob) that I have, so it literally is a case that it seems to be an issue with the background image file.
//#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
mGLSurfaceView = new CCGLSurfaceView(this);
CCDirector.sharedDirector().setScreenSize(CCDirector.sharedDirector().winSize().width,
CCDirector.sharedDirector().winSize().height);
CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
CCDirector.sharedDirector().getActivity().setContentView(mGLSurfaceView, createLayoutParams());
InitParam();
getAdmob();
}
Activity game xml is;
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".GameActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY" />
</RelativeLayout>
width & height of the imageview is "wrap content" however the scaletype is "fitXY".
thats kind of saying 2 different things to the device.
if that image should be full screen change it's width & height to match/fill parent.
Check onCreate(), onStart() and onResume() methods in your code, you might be doing some time consuming, blocking tasks. If so, load heavy background tasks on a separate thread.
Profile your code with Traceview and dmtracedump to check what is taking more time.
In the market I saw an app is displaying admob in the alert dialog . after I reading this and this and this ,
I still can't figure out how to display the ads in alert dialog same like the one below. (or any others method , that can display the ads in a alert dialog)
I've been scratching my head trying to figure out how
to do that.
Could anyone be kind enough to guide me through how
to go about this?
XML :
<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="#id/tablayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="*"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res/com.xxx.xxx">
<TableRow>
<com.admob.android.ads.AdView android:id="#id/myad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
myapp:backgroundColor="#ff000000"
myapp:primaryTextColor="#ffffffff"
myapp:secondaryTextColor="#ffcccccc" />
</TableRow>
</TableLayout>
Java :
public void onBackPressed() {
//set up dialog
Dialog dialog = new Dialog(main.this);
dialog.setContentView(R.layout.exitdialog);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
//now that the dialog is set up, it's time to show it
dialog.show();
}
});
}
Thanks.
I found another solution to do this.
AdMob offers a JavaScript integration solution for Android and iPhone sites/web apps.
Instead of using XML and a custom dialog.
We can use Admob web ads and a HTML that contain the admob code and load it with webview in the dialog.
I believe it's not possible unless you're on landscape mode. The reason is that the ad needs something close to the device width (in portrait mode, e.g. 480) for the ad, so it is only available in landscape mode when you're adding it inside a dialog
I'm having troubles trying to style my tabs in android.
I want to make them look exactly the same as whats in the open source android contacts list (see https://android.googlesource.com/platform/packages/apps/Contacts
).
Problem is that when they display on the screen it looks a bit different to the contacts app.
When it should look like this:
Notice how the background colors are a little bit different and the text colors are different.
Not sure why this is the case as its basically the same code and icons.
My tab layout code is the following:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="0dp" />
</LinearLayout>
</TabHost>
Which doesn't contain anything special there.. and the TabActivity is as follows:
public class TabbedActivity extends TabActivity implements
TabHost.OnTabChangeListener {
private TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tab);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(this);
setupLatestTab();
setupSavedTab();
tabHost.setCurrentTab(0);
}
private void setupLatestTab() {
Intent intent = new Intent().setClass(this, ResultsActivity.class);
tabHost.addTab(tabHost
.newTabSpec("latest")
.setIndicator("Latest",
getResources().getDrawable(R.drawable.ic_tab_recent))
.setContent(intent));
}
private void setupSavedTab() {
Intent intent = new Intent().setClass(this, ResultsActivity.class);
tabHost.addTab(tabHost
.newTabSpec("saved")
.setIndicator("Saved",
getResources().getDrawable(R.drawable.ic_tab_starred))
.setContent(intent));
}
#Override
public void onTabChanged(String tabId) {
// Because we're using Activities as our tab children, we trigger
// onWindowFocusChanged() to let them know when they're active. This may
// seem to duplicate the purpose of onResume(), but it's needed because
// onResume() can't reliably check if a keyguard is active.
Activity activity = getLocalActivityManager().getActivity(tabId);
if (activity != null) {
activity.onWindowFocusChanged(true);
}
}
}
I am using the same images from the drawable folders too.
I know i can set the background of tabs manually by doing something like this in the tabactivity
tabHost.getTabWidget().getChildAt(index).setBackgroundColor(Color.parseColor("#ff202020"));
But the contacts app isn't doing this sort of thing anywhere (most of the top tab code is in DialtactsActivity), so just want to do what the open source app is doing when displaying tabs - i'm not sure how and why the contacts application tabs look much better when im basically using the same code and resources.
I guess im just missing something trivial??
This turned out to be a problem with my minimum android version not being specified ..
Added:
<uses-sdk android:minSdkVersion="8" />
to the androidmanifest and it worked fine. I guess it was reverting to the old tabs look and feel in earlier versions of android.