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.
Related
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
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 been looking around this for a while now and could not find solution;
When you create ProgressDialog, the Main View is turned to grey and loses focus,What I want to do is to keep an element from the Main View focused (lets say Admob) and keep it usable while the ProgressDialog is shown
UPDATE : CODE !
View mainview = LayoutInflater.from(MainAct).inflate(R.layout.activity_main, null);
View adView = mainview.findViewById(R.id.adView);
mProgress = new ProgressDialog(MainAct);
mProgress.setMessage("Downloading...");
mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgress.setCancelable(false);
mProgress.setCanceledOnTouchOutside(false);
mProgress.show();
adView.requestFocus();
it seems like using reaquestFocus doesn't work and is only intended for form elements, not bringing elements on TOP
UPDATE 2:
<?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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linlaHeaderProgress"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone" >
<ProgressBar
android:id="#+id/pbHeaderProgress"
style="#style/Spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp" >
</ProgressBar>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="left|center"
android:padding="2dp"
android:text="Loading...."
android:textColor="#F1F1F1"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:cacheColorHint="#android:color/transparent"
android:divider="#000000"
android:dividerHeight="0dp"
android:fadingEdge="none"
android:persistentDrawingCache="scrolling" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:padding="10dp"
android:textColor="#f1f1f1"
android:textSize="15sp"
android:textStyle="bold"
android:visibility="gone" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom|center"
android:orientation="horizontal" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="MY_AD_UNIT_ID"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, MY_TEST_DEVICE_ID" />
</LinearLayout>
</LinearLayout>
The code above is for a layout that is used almost exclusively in own of my apps. What I do while loading the data is quite simple.
Look at how the ProgressBar is defined in the XML. While still loading data, I change the LinearLayout linlaHeaderProgress to VISIBLE which effectively hides the ListView below it because of the attributes set to it. After I am done loading the data, I change the visibility of the LinearLayout linlaHeaderProgress to GONE. A point to note here is, the Admob XML right at the bottom of the XML, is always visible to the user and can be interacted with anytime before or after the data is loading / done loading.
If you need to show this XML in the form of a Dialog, you can always do so by attaching a Theme to this activity.
Hope some of this helps in your endeavour.
UPDATE
Check these tutorials. These are a few I could find with a quick search for showing a Horizontal ProgressBar. You are using a ProgressDialog.
http://www.techrepublic.com/blog/app-builder/androids-batterymanager-and-progressbar-two-for-one-tutorial/703
http://theandroid.in/example-of-progressbar-in-android/
And finally, use this Google Search to see a few examples of making an activity look like a dialog.
This should help you achieve what your screenshot suggests.
I have spent hours on this problem, I hope somebody can help me.
Sorry in advance if the question is stupid, I am fairly new to android development and also, I am using an app builder which has generated already a lot of code. I am basically trying to tweak this code with what I want...
The question is just about xml layouts, I don't think it involves code. I want to display a simple button at the bottom of this screen.
So I have this screen which builds a group of buttons in a grid. I also display an ad banner at the top. The parameters of these buttons are specified through the web app builder on a web interface. No problem with these buttons. They are displayed correctly in this screen layout below:
<?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/containerView"
android:background="#android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="123"
ads:adSize="SMART_BANNER"
ads:testDevices="123"
ads:loadAdOnCreate="true">
</com.google.ads.AdView>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/verticalScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TableLayout>
</ScrollView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerViewHorizontal"
android:background="#android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerViewHorizontalBottom"
android:background="#android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerHorizontalButtons"
android:background="#android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
Now what I am trying to do, is to add at the bottom of this group of buttons, a button that does not belong to the list generated through the web app builder, but manually created:
<Button
android:id="#+id/bmenu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
I have already taken care of the code for this button, it works well, but I don't manage to display it at the bottom of my screen.
I guess it has to do with the general layout of the screen. I have to admit I am completely lost in the series of layout nested in one another, I don't understand what is exactly doing what.
If that helps, I have managed to display my button at the top of the screen, even just below my ad banner. But not at the bottom.
Also if that helps, I guess one of the layout takes up the whole heightspace (the scrollview?), living 0 height for the button I want to add...
Thanks a lot for your time and if you can help me!
Juyt implement a Linear layout instead of a relative one for the list and the button.
Then give the list a height of 0dp and a weight of 1
The button will have whatever attributes:
<LinearLayout 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"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
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.