To summarize my problem:
I have a RecylerView that I use inside SwiperRefreshLayout.
Except for these two, I want to add a banner adview to the bottom of the screen.
When I add the tag com.google.android.gms.ads.AdView above SwipeRefreshLayout it looks like this:
but when I add the adview's tag below SwipeRefreshLayout it disappears. Only Recylerview appears on the screen.
There isn't adview:
How can I pin the ad to the bottom of the screen?
my codes are like this:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/blur"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_above="#+id/adView"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycleView"
android:layout_above="#+id/adView"
android:layout_margin="2dp"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
android:layout_above="#+id/swipeRefreshLayout" />
</LinearLayout>
Related
I want to fix bottom navigation and ads to the bottom. But the picture remains as seen. Can you help me?
My xml code is as follows
enter image description here
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:gravity="bottom"
android:layout_gravity="bottom"
android:layout_height="match_parent"
android:layout_below="#+id/bottom_nav"
android:background="#drawable/bgapp"
android:orientation="vertical"
tools:context=".Anasayfa"
tools:ignore="NotSibling">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-7657308039754206/7261639976"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:itemBackground="#drawable/altbottom"
app:itemIconSize="25dp"
app:itemIconTint="#drawable/itemcolors"
app:labelVisibilityMode="auto"
app:menu="#menu/bottom_nav_menu"
tools:ignore="MissingConstraints"/>
</LinearLayout>
You didn't paste the whole xml, but I guess it's just ordering problem.
You need to move the AdView and BottomNavigationView to be last in the file.
I'm making an android app but having trouble with positioning a banner.
I have an Activity acting as a container for fragments. I basically want a Scroll View for the root, then the fragment, and an ad on the very bottom.
Here is my XML.
<ScrollView 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>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:layout_constraintBottom_toBottomOf="parent"
ads:layout_constraintStart_toStartOf="parent"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintTop_toBottomOf="#id/fragment_container"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxx"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Here's how the layout looks like
Basically, the ad is placed directly below the fragment container and it isn't attaching to the viewport's bottom.
I want it to attach to the bottom, then have the scroll view scroll both the fragment + ad together.
This is the only way I could get the ad to appear on the bottom (in short terms):
<Constraint layout>
<Scroll View>
<Fragment container>
<Ad constraining to parent bottom>
However, with this method, the scroll view would only scroll the fragment and doesn't include the ad, meaning you'll get the ad overlapping the fragment content sometimes.
So I think the scroll view must be the root view. However, I'm having trouble managing the child to have the ad appear on the scroll view's bottom, instead of the children's bottom edge.
You can do it this way using relative 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/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxx"
android:layout_alignParentBottom="true"/> <!--just add this line-->
</RelativeLayout>
Greets by Nice! Can you try it with RelativeLayout ;)
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxx"
android:layout_alignParentBottom="true"/> <!--just add this line-->
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
ads:layout_constraintTop_toTopOf="parent"
ads:layout_constraintStart_toStartOf="parent"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintBottom_toTopOf="#id/adView2">
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxx"
ads:layout_constraintBottom_toBottomOf="parent"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Change your xml with below code
<androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true"
android:layout_weight="1">
<androidx.appcompat.widget.LinearLayoutCompat
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<com.google.android.gms.ads.AdView
android:id="#+id/adView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-xxxx" />
</androidx.appcompat.widget.LinearLayoutCompat>
Please check below image It's same as you want
I hope this can help you!
Thank You.
I have added admob to my app and placed the ad at the bottom of the page.... hoever it covers the web content that i need people to see.... is there anyway to place the ad in a separate container or frame at the bottom of the screen so that the content stops where the ad begins?
here is my xml file
<?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: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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.payforlikes.app.payforlikes.MainActivity"
tools:showIn="#layout/activity_main">
<WebView
android:id="#+id/activity_main_webview"
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="ca-app-pub-4075500144464557/1187222817"/>
</RelativeLayout>
You should be able to rearrange your XML as follows:
<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="ID"/>
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/adView />
You move the WebView below the AdView in your XML so you can reference the AdView's ID, and then, using your RelativeLayout, you can restrict the WebView to be above the AdView.
I am using recycle view to show the list from Android studio and its working fine when it is only element like below.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 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:id="#+id/list"
android:name="com.example.ItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="com.example.ItemFragment"
tools:listitem="#layout/fragment_item" />
But I want to show Google Ads below the recyclerview, so here is the modified code, which is not showing the recyclerview at all but displaying only Ads.
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<android.support.v7.widget.RecyclerView 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:id="#+id/list"
android:name="com.example.ItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="com.example.ItemFragment"
tools:listitem="#layout/fragment_item" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView1"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
I am wondering, is it not possible to add sibling elements to Recyclerview, or its not possible to place Recyclerview inside other layouts.
Try This. You also need to set adapter to recyclerview
<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">
<com.google.android.gms.ads.AdView
android:id="#+id/adView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:name="com.example.ItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_above="#id/adView1"
app:layoutManager="LinearLayoutManager"
tools:context="com.example.ItemFragment"/>
</RelativeLayout>
It's possible to add sibling elements to a RecyclerView, but it looks like you have an invalid Layout. You need to try something like:
<LinearLayout>
<RecyclerView/>
<AdView/>
</LinearLayout>
Right now, you have a closing RelativeLayout tag, but no starting one.
Check the condition in the onCreateView()
If it contains a condition like ->
if (view instanceof RecyclerView) {
... }
Just remove it or use appropriate condition.
I want to display a simple banner ad in RecyclerView which loads a list of images. just a simple bottom banner ad.
This is my RecyclerViewXML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
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:id="#+id/list"
android:name="com.example.fragmentactivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAC873"
app:layoutManager="StaggeredGridLayoutManager"
tools:context="com.example.fragmentactivity"
tools:listitem="#layout/fragment_image" />
if I try to put to the recyclerView inside RelativeLayout or any other so i could include banner in the bottom, but then it wont display any images.
How can I display banner in that view?
I tried almost all the available solution to dynamically add the banner but nothing worked
You should add the banner to the bottom and in the front of the RecyclerView. For do that this will help you:
Usage: Replace the RelativeLayout for your AdMobView
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:name="com.example.fragmentactivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAC873"
app:layoutManager="StaggeredGridLayoutManager"
tools:context="com.example.fragmentactivity" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="52dp"
android:background="#fff"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Explanation: . The RelativeLayout is bellow RecyclerView and it is android:layout_alignParentBottom="true" overlaying the RecyclerView.
I have it working perfectly well by first adding the adview at the bottom and then adding the recycler view above it (leaving a margin at the bottom for the adview that was placed earlier and then filling the parent with the recycler view). My layout is as follows:
Note: In my case it is MoPubView Replace it with your Adview from Admob.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#color/color_green"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_category_list"
tools:context=".CategoryListActivity">
<!-- I'm using Mopub, replace the MoPubView with AdView in your case -->
<com.mopub.mobileads.MoPubView
android:id="#+id/adview"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" />
<android.support.v7.widget.RecyclerView
android:id="#+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:layout_marginBottom="60dp" />
</RelativeLayout>