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
Related
I have a big problem.
Im using a View in FrameLayout which I rotate with canvas.rotate. But on LG L9 my Adview also rotates in the same direction and it shouldnt rotate.
On Xpera Mini St15i it doesnt rotate.
Android is so buggy.
Im using normal OnDraw method in my CustomView
Heres my XML file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<com.example.easymeasure.CustomOpenCVJavaCameraView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:id="#+id/OpenCVView"
opencv:show_fps="false"
opencv:camera_id="any" />
<com.example.easymeasure.CustomView
android:id="#+id/CustomView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/LinearLayout_camera_calibration"
android:visibility="gone"
>
<Button
android:id="#+id/Button_cancel_camera_calibration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button_cancel_camera_calibration"
android:layout_gravity="left|bottom"
android:layout_margin="10dp"
android:onClick="Button_cancel_quick_hardware_test_onclick"
/>
<Button
android:id="#+id/Button_start_camera_calibration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button_start_camera_calibration"
android:layout_gravity="right|bottom"
android:layout_margin="10dp"
android:onClick="Button_start_quick_hardware_test_onclick"
/>
</LinearLayout>
<RelativeLayout
android:orientation="vertical"
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_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
ads:adUnitId="ca-app-pub-3227530058034046/1468436616"
ads:adSize="SMART_BANNER"/>
</RelativeLayout>
</FrameLayout>
I have found out how to solve the problem. I had to move my AdView in xml file before the CustomView with the Ondraw method. Anyway I think this behaviour isnt normal.
Edit:
Now Ive found out that other Frames are also moving if they laying above the CustomView in hierarchy. Until now I cant explain why there moving and how to remove this behaviour in Frames laying on top of the hirarchy.
Edit2:
Ive found out whats causing this undefined behaviour. It happens if canvas.restore() gets more often called than canvas.save()
I want to add second ads banner inside my LinearLayout but my app crashes when insert the second banner.Works well with the external banner into RelativeLayout,but i want to add second...inside ScrollView. What can i do?Here is my code:
<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="fill_parent"
android:layout_height="fill_parent"
android:background="#000000" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
/>
<ToggleButton
/>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxxxxxxx"
ads:loadAdOnCreate="true"
>
</com.google.ads.AdView>
</LinearLayout>
</ScrollView>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="xxxxxxxxx"
ads:loadAdOnCreate="true"
>
</com.google.ads.AdView>
</RelativeLayout>
Do NOT displaying multiple Admob Adviews on a single page. It is against the terms of service. you signed up to Admob with. Unless you want to have your Admob account suspended.
You can add unlimited advertisements on your page . I don't think there is any limit for that. But its not recommended to do, according to admob rules there must be one advertisement on one page. And for every Adview you can need to define its seperate Layout that can be linear or relative.
Your application is crahing because you didn't set your TextView and ToggleButton Layout parameters. Remove them or set their Layout properties. Like
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I have SurfaceView with setZOrderOnTop(true) .This flag is needed for the transparent SurfaceView's background during the drawing process.
How can I display any View in front of it?
SurfaceViews are not designed to be composited with other views. As described in the doc for setZOrderOnTop, once you set this mode, none of the other window contents will be visible on top of the SurfaceView.
If you want to do compositing, you will have to do it yourself, off-screen, then draw the results into the SurfaceView.
In my experience, that still work.
But if you use a RelativeLayout, it will be ugly.
<FrameLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.applicationtest.MainActivity" >
<SurfaceView
android:id="#+id/surfaceView1"
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"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
<LinearLayout
android:layout_marginTop="50sp"
android:orientation="vertical"
android:background="#44444444"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:text="ttttttttttttttttttttt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="ttttttttttttttttttttt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="ttttttttttttttttttttt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
The WallPaperService are usung SurfaceView.
So there is no reason that no view can't be on the top of SurfaceView...(I guess??)
How do I prevent my Android screen from shifting down when a page is view is displayed and receives the first add?
When the view is first displayed the screen is normal, then a ad is displayed and the screen shifts down a few lines.
Here is my code.
<?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:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background3"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_gravity="top"
ads:adSize="BANNER"
ads:adUnitId="xxxxxxxxxx"
android:gravity="center"
ads:loadAdOnCreate="true" >
</com.google.ads.AdView>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="34dp" >
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Start" />
</TableRow>
</LinearLayout>
TIA, Trey
I don't have much experience with the ads. But from strictly a View standpoint it seems like what is happening is that the adView is set to View.GONE before it loads. Which means that its space is ignored when placing the other views into the parent.
I don't know for sure how you could get it to not do that. But I would try explicitly setting it to android:visibility="invisible" in your xml, then in your java you'll have to make it visible after it has loaded. "invisible" is like gone, but it tells the parent to respect the space that you will take up when you are visible.
Perhaps the loadOnCreate attribute causes the behavior you are seeing? Might also be worth trying it without that and just load it manually in your onCreate.
What I am trying to do is build a layout which if an AdMob Ad fails to load it shows one of my own Ads (ImageView) which are built into the app.
I cant seem to get the listview not to cover up my Ad.
I have no issue getting it to work if I just use the AdMob Ad alone.
Here is my layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bgdark"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/actionbarLayout"
android:layout_width="fill_parent"
android:layout_height="#dimen/actionbar_height"
android:layout_alignParentTop="true"
android:layout_marginBottom="2dip"
android:background="#drawable/actionbar_background" >
//Top bar is here
</RelativeLayout>
<LinearLayout
android:id="#+id/layoutAds"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#00FF00"
android:layout_alignParentBottom="true"
android:layout_centerVertical="true" >
<ImageView
android:id="#+id/ImageAlternativeAd"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_gravity="center_vertical"
android:src="#drawable/anad"
android:visibility="visible" >
</ImageView>
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="123456abcd"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR,CFFFFFFF9A836FD749F3EF439CD"
android:visibility="visible" />
</LinearLayout>
<ListView
android:id="#+id/listCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/adView"
android:layout_below="#id/actionbarLayout"
android:cacheColorHint="#00000000" >
</ListView>
This is exactly what you want.
https://stackoverflow.com/a/5188181/563306
Also, did you try with,
<ListView
android:id="#+id/listCategories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/layoutAds"
android:layout_below="#id/actionbarLayout"
android:cacheColorHint="#00000000" >
</ListView>
I think a clean way to implement this would be to have a component that initially holds your com.google.ads.AdView view. Then, when you notice that your ad did not load, you can swap it with your ImageView on the fly.
Even better: make that a custom component. So from the "outside" (your layout) you are just including an instance of this component and internally, this component is implemented by either displaying an ad from AdMob or your own ad depending on whether the AdMob ad fails to load or not.