AdbMob show white screen - android

My adb in Activity works good.But when I'm using include.But when I'm using in fragment it's doesn't show.What I doing wrong?
<include
android:id="#+id/adMob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/elm_adb_mob"
tools:ignore="NestedWeights" />

Why do you include a layout? Just create the AdView instead of the include.
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"/>
Read more here.

Related

How to make the ads appear on an activity in android?

I have an android activity with a xml layout as follows:
<RelativeLayout>
...
<GridLayout>
...
</GridLayout>
....
</RelativeLayout>
to which I would like to add the ads-thingy. I have tried the following setup:
<LinearLayout>
<RelativeLayout>
...
<GridLayout>
...
</GridLayout>
....
</RelativeLayout>
<com.google.android.gms.ads.AdView>
...
</com.google.android.gms.ads.AdView>
</LinearLayout>
but it does not show the ads (the dots represent other content I believe is not important for this question). Is there some special trick to use to be able to show the ads-thingy? Make the layout even more complex? Is there a special layout-things I have to use?
My main activity does show the ads; the structure here is as follows:
<LinearLayout>
...
<android.support.design.widget.CoordinatorLayout>
<LinearLayout>
...
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
<com.google.android.gms.ads.AdView>
....
</com.google.android.gms.ads.AdView>
<LinearLayout
A more complete example of the real layout xml file (stripped off the really uneccessary parts...)
<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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
>
<GridLayout
android:id="#+id/grid"
android:layout_margin="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/new_cancel"
android:alignmentMode="alignBounds"
android:columnCount="10"
android:columnOrderPreserved="false"
android:useDefaultMargins="true">
// REMOVED TEXTVIEW, EDITTEXT, IMAGEBUTTONS etc HERE
</GridLayout>
<Button
android:id="#+id/new_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#color/colorPrimaryDark"
android:text="Cancel"
android:layout_margin="5dp"
/>
<Button
android:id="#+id/new_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="#color/colorPrimaryLight"
android:text="Ok"
android:layout_margin="5dp"
/>
</RelativeLayout>
<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>
</LinearLayout>
P.S. What is the general name of things like LinearLayout or RelativeLayout?
As far as I know you have to initialize your ad in your Activity code using an AdRequest object:
AdView adView = (AdView) findViewById( R.id.adView );
AdRequest request = new AdRequest.Builder()
.build();
adView.loadAd( request );
Discarding a problem with your ADS initialization, as you say it works in another activity, and focusing on layout problems, I'd say the problem is the following:
You have a Vertical LinearLayout that covers the whole screen (match_parent on a top level) with two childs
The first child is a RelativeLayout that covers the whole parent (match_parent)
The second child is the Ad View that is probably being displayed, but you can't see it because it starts at the first pixel after the screen height.
So to correct this, I'd suggest you to try:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#cccccc"
>
.
.
.
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
I'd also suggest to give the proper dimensions to the Ad View as you know them (https://developers.google.com/android/reference/com/google/android/gms/ads/AdSize#constant-summary) . Then, I'd suggest to use a FrameLayout as is way lighter than a linearlayout. You would do it like this:
<FrameLayout
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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
android:layout_marginBottom = "50dp">
.
.
.
</RelativeLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="bottom|center_horizontal"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</FrameLayout>
Make sure that your Admob layout is displaying in xml view. Put your admob view inside RelativeLayout and try to use android:alignparentBottom:true
You can check this answer.
https://stackoverflow.com/a/28578320/2022000

Element gets pushed to the right. Why?

When my LinearLayout is coded like this:
<LinearLayout ...
<!-- SOME CODE HERE -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="17">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/inputText"
android:inputType="textCapCharacters|textMultiLine"
android:hint="#string/input_text_hint"
android:gravity="top|left"/>
</LinearLayout>
<!-- MORE CODE HERE -->
</LinearLayout>
My activity looks like this:
But when I add this adMob code:
<LinearLayout ...
<!-- SOME CODE HERE -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="17">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="#string/top_banner_1">
</com.google.android.gms.ads.AdView>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/inputText"
android:inputType="textCapCharacters|textMultiLine"
android:hint="#string/input_text_hint"
android:gravity="top|left"/>
</LinearLayout>
<!-- MORE CODE HERE -->
</LinearLayout>
I get this:
How do I get the ad to be on top of the EditText?
The default orientation for a LinearLayour is horizontal, which makes the add pushing your EditText to the right. If you add android:orientation="vertical" to your parent LinearLayout the issue is solved.
I think that the correct code you have to use is:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="17">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="#string/top_banner_1">
</com.google.android.gms.ads.AdView>
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/inputText"
android:inputType="textCapCharacters|textMultiLine"
android:hint="#string/input_text_hint"/>
</LinearLayout>
I have removed some properties that can make your layout look ugly in the image you provided.

ScrollView does not show in relativelayout

This is what I am trying to implement:
The visibility of the adview can be toggled to either visible or gone. This is a layout which I used and is working without the two buttons.
This is the xml for with the scrollview and the adview, without the buttons. (scrollview works)
<RelativeLayout
xmlns:ads="http://schemas.android.com/apk/res-auto"
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">
<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"
android:visibility="visible" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/adView"
android:paddingBottom="12dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="12dp">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:layout_marginTop="0dp"
android:stretchColumns="0">
<TableRow>
//scrollable content here
</TableRow
</TableLayout>
</ScrollView>
I try to implement scenario A by encapsulating the adView element inside a TableLayout, so I can attempt to use TableRows to implement the buttons.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tableLayout">
<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"
android:visibility="visible" />
</TableLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/tableLayout"
.....
When I use this approach, my scrollview disappears. Where am I going wrong and how can I resolve this and get to scenario A?
You are missing the attribute 'android:layout_alignParentBottom="true"' from TableLayout that you had in AdView. This aligns the table in top of the parent relative layout although your intention was to put it in the bottom.

#+id/adView" is red, why?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
android:background="#ff000640">
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_above="#+id/adView"
/>
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub"
ads:adSize="BANNER"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
Why is it red? When I run the app the adview works just fine so I have no idea why it wouldn't be working. The red part also disappears for a short while once I run the app.
when I change above to below it gives no error
#+id/adView : the + sign means to generate a new id if it does not exist yet.
I think it would be better to
<WebView ...
android:layout_above="#id/adView"
meaning to refer to another component with id adView
and as Krishnabhadra suggests, put the AdView declaration first.
#+id/ is used to create an id. To reference it use #id/. Also put the code for the adview above the code for the webview, as the id needs to be defined, before you can reference it.
Try this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity"
android:background="#ff000640">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="ca-app-pub"
ads:adSize="BANNER"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_above="#id/adView"
/>
</RelativeLayout>
I think you didn't import Google Admob Library. Please import this library into your project and try again. Click Here
Because when I try your code, any red lines appear. Just import the library.
Try File->Invalidate Caches.... I've run into this before when AS doesn't re-generate the data bindings when editing layouts. Same thing sometimes happens for new string resources.

Admob on my Custom SurfaceView

I have created a my custom SurfaceView and now want to place admob on surface view. Not below or above because it reduces the screen size. I found many questions here but all are solving by putting SurfaceView below or above of the admob view. but I want to put admob on SurfaceView.
XML code of my Activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity" >
<com.scratchview.ScratchView
xmlns:wsv="http://schemas.android.com/apk/res-auto"
android:id="#+id/scratch_view_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
wsv:antiAlias="true"
wsv:revealSize="25dp"
wsv:scratchable="false" />
<com.scratchview.ScratchView
xmlns:wsv="http://schemas.android.com/apk/res-auto"
android:id="#+id/scratch_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
wsv:antiAlias="true"
wsv:revealSize="25dp"
wsv:scratchable="true" />
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="top"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/admob_id"
ads:loadAdOnCreate="true" />
</RelativeLayout>
Note: I have tried by putting Admob below and above it is working fine. I need to put admob onto SurfaceView

Categories

Resources