I am trying to make the ads look as part of the app by placing them inside a view with a custom background. This is the result that I want to achieve - actually it's a preview from the layout editor:
When I run the app, however, the background and the "Sponsorized" text are not shown. How can I make it work?
This is my current layout for the view:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="#string/admob_id"
ads:adSize="MEDIUM_RECTANGLE"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sponsorized"
/>
</LinearLayout>
Create an another inner LinearLayout as parent to AdView . Use this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_margin="10dp"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="#string/admob_id"
ads:adSize="MEDIUM_RECTANGLE"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sponsorized"/>
</LinearLayout>
Related
For those wanting to have an adView in their app that is comprised of Fragment layouts, hope the below helps you!
Basically, I was originally trying to place the adView inside the Layout XML for each fragment. This caused the adView to either be pushed off the screen or not play nice with the relative layout commands (ex. alignParentBottom).
The solution was to move the adView into the Main Activity Layout outside of the the Coordinator Layout used for my fragments. Then I wrapped the Coordinator Layout and adView in a Relative Layout.
This then allowed me to fully control the adView and present on each fragment pinned to the bottom of the screen.
I have found that when I change the background of your ScrollView, it paints OVER the AdView. You MUST place your AdView below the ScrollView in code. That way, it is drawn AFTER the ScrollView and is therefore on top.
Here is your edited code:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<!-- Dummy item to prevent EditTextView from receiving focus -->
<LinearLayout
android:id="#+id/dummyLayout"
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="horizontal" />
<!-- Dummy item to prevent EditTextView from receiving focus -->
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="4"
android:hint="#string/hint"
android:nextFocusLeft="#id/editText1"
android:nextFocusUp="#id/editText1"
android:singleLine="true"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linearLayout1"/>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textView4"/>
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/button1"
android:background="#color/colorAccent0"
android:stretchColumns="*"/>
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tableLayout1"
android:layout_above="#+id/adView"
android:background="#color/colorGray">
<TableLayout
android:id="#+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"/>
</ScrollView>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
I have an activity as follows.
I launch it inside another activity using setContentView(R.layout.below); However, below.xmlcontains a pager (which is a fragment and has its own textview, webview etc.). My problem is even though the Google Ads block is shown in the graphical layout on Eclipse and NO error is thrown when I run the java code shown below; I CAN NOT see the ad. I don't understand what is happening! I've tried so many things, including changing this layout to a simple relative layout. The following is how I call this adView.
setContentView(R.layout.below);
AdView mAdView2 = (AdView) findViewById(R.id.adView2);
AdRequest adRequest2 = new
AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
mAdView2.loadAd(adRequest2);
This is the actual XML file that contains this adView.
<?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"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<LinearLayout
android:id="#+id/home_layout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="#+id/footerLayout">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:id="#+id/footerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
>
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="0dp"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</LinearLayout>
</RelativeLayout>
try removing the padding of RelativeLayout, also happens to my app, but got it working after removing the padding
<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"
android:orientation="vertical"
android:paddingLeft="5dp" <--- REMOVE
android:paddingRight="5dp" > <--- REMOVE
if you must use a padding, add it on parent layout of the adview, so it will be on LinearLayout
Give some heights to ad view it's height depends on device screen size it can be 50dp,120dp
use this if nothing happens then please put different size and try one more time
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/home_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_above="#+id/footerLayout">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout
android:id="#+id/footerLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
>
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginBottom="0dp"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</LinearLayout>
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.
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.
Well before my ads were working fine and were always at the bottom of the screen, but now I added a scrollview and the ads move up a little bit to be right under my scrollbar.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/pattern_carbon_fiber_dark">
<ScrollView android:id="#+id/ScrollView01"
android:layout_width="wrap_content" android:layout_height="339px">
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:text="Silent Mode" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/silent_textview"></TextView>
<ToggleButton android:layout_height="wrap_content" android:text="Silent Mode" android:id="#+id/silentbutton" android:layout_width="wrap_content" android:textOff="OFF" android:textOn="ON"></ToggleButton>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Wifi " android:id="#+id/wifi_textview"></TextView>
<ToggleButton android:layout_width="wrap_content" android:text="ToggleButton" android:layout_height="wrap_content" android:id="#+id/wifibutton"></ToggleButton>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/gps" android:id="#+id/gps_textview"></TextView>
<Button android:text="#string/gpsbutton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/gps"></Button>
<TextView android:layout_width="wrap_content" android:text="#string/bluetooth" android:layout_height="wrap_content" android:id="#+id/bluetooth_textview"></TextView>
<ToggleButton android:layout_width="wrap_content" android:text="ToggleButton" android:id="#+id/bluetooth" android:layout_height="wrap_content"></ToggleButton>
<TextView android:text="#string/brightness" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/screenbrightness_textview"></TextView>
<SeekBar android:layout_height="wrap_content" android:id="#+id/SbBar" android:layout_width="fill_parent" android:max="255"></SeekBar>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="wrap_content">
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/adView"
android:layout_width="fill_parent"
ads:adSize="BANNER"
ads:adUnitId="a14ddb1a61b9fb4"
ads:loadAdOnCreate="true"
android:visibility="visible"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
android:layout_alignParentTop="false">
</com.google.ads.AdView>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Please help, thanks!
EDIT: Picture
UI pic http://img52.imageshack.us/img52/9397/unledyv.png
1) You will probably leave the ads outside the scollview so they are always visible (as opposed to scroll out of the screen)
2) What is the purpose of the relativelayout surrounding the adview? That is the only view inside it: just remove it and you will get the same layout, but better performance.
Based on the update, I suggest you to try this layout (you will need to fill many blanks):
<RelativeLayout
...
>
<AdView
android:id="#+id/adView"
android:layout_alignParentBottom="true"
...
/>
<ScrollView
android:layout_alignParentTop="true"
android:layout_above="#id/adView"
...
>
<LinearLayout
...
>
...
...
</LinearLayout>
</ScrollView>
</RelativeLayout>
The basic idea is to put the AdView ad the bottom of the screen using a RelativeLayout, and then setting the ScollView to be placed above the ads (so it does not block them) and aligned to the top of the screen.
Why don't you try using a relative layout as the root layout.
<RelativeLayout>
<LinearLayout
android:layout_alignParentTop="true">
ETC
</LinearLayout>
<com.google.ads.AdView
android:layout_alignParentBottom="true">
</com.google.ads.AdView>
</RelativeLayout>
To improve the user experience, we can move the ad outside the ScrollView and use a RelativeLayout to pin the ad to the bottom of the screen. This is the needed XML markup:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView android:id="#+id/scrollLayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_above="#+id/adView">
<LinearLayout android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Place very long string here..."/>
</LinearLayout>
</ScrollView>
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR"
ads:loadAdOnCreate="true"/>
</RelativeLayout>