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.
Related
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>
i want to have a recycle view and a fix button in bottom of that.
i use coordinator layout and everything is ok but when scroll of recycle view ended, the fix button move a little with recycle view.
what is the problem??
This is my code..
<?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="match_parent"
android:background="#drawable/filter_frame"
android:orientation="vertical">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/shopsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/complete_info_order"
android:layout_marginBottom="#dimen/_70sdp"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<Button
android:id="#+id/complete_info_order"
android:layout_width="match_parent"
android:layout_height="#dimen/_40sdp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginLeft="#dimen/_20sdp"
android:layout_marginTop="#dimen/_30sdp"
android:layout_marginRight="#dimen/_20sdp"
android:layout_marginBottom="#dimen/_60sdp"
android:background="#drawable/login_button_frame"
android:gravity="center"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
what should i do to button don't move with recycle view??
Thanks a lot...
Try this instead:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/shopsList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:id="#+id/complete_info_order"
android:text="Button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="40dp"/>
</RelativeLayout>
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 am trying to achieve something like following
I am trying to build an activity joining 3 fragments. Top fragment contains a search bar (EditText), middle fragment has a Banner Slider (ViewPager), and the bottom fragment contains a GridView loaded with n number of items dynamically. I want to make the middle & bottom fragments scrollable vertically while the top one being fixed. But the Grid view is scrolling within itself while the height is set to wrap_content.
If the GridView is given a fixed height, it is working as expected. But I cannot give it a fixed height as its content is dynamically loaded.
Home Activity:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:background="#color/colorLightGray"
tools:context=".HomeActivity">
<ScrollView
android:id="#+id/scrollableContents"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:padding="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragmentContainerSearchBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="#+id/fragmentContainerSlider"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="#+id/fragmentContainerPopularItems"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
SearchBarFragment:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragments.HomeSearchBarFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingHorizontal="2dp"
android:paddingVertical="2dp"
android:focusable="true"
android:focusableInTouchMode="true">
<EditText
android:id="#+id/etSearchBoxHome"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="40dp"
android:drawableRight="#drawable/ic_search_24dp"
android:drawablePadding="5dp"
android:background="#drawable/et_search_home"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_marginTop="0dp"
android:clickable="true"
android:cursorVisible="true"
android:focusable="true"
android:textSize="14dp"
android:hint="#string/home_searchbox_hint"/>
</RelativeLayout>
</FrameLayout>
Banner Slider Fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="2dp"
tools:context=".fragments.HomePromoSliderFragment">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
card_view:cardBackgroundColor="#color/colorWhite"
card_view:cardCornerRadius="3dp">
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</android.support.v7.widget.CardView>
</RelativeLayout>
Popular Items Fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragments.HomePopularItemsFragment">
<GridView
android:id="#+id/gvPopularItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="150dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth">
</GridView>
</RelativeLayout>
You can take nestedScrollView outside of ViewPager and Gridview.
Set the recycler View to setNestedScrollingEnable(false).
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent">
</GridView>
</android.support.v4.widget.NestedScrollView>
Try using `Constraint Layout` for whole layout, `autocomplete text view` for search bar, `viewpager` for banners, recycler view for showing the loaded items.
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<AutoCompleteTextView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"/>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
** Set the margins as per the UI
Set your layout manager as Gridlayout manager for recycler view as below:
recyclerview.layoutManager = GridLayoutManager(this, 2, OrientationHelper.VERTICAL, false)
Hope it will work fine. Let me know if you find any issues.
i'm trying to put webview and admob banner in one xml but the thing is when i do they get mixed up i opened it in another device with different screen size. this is my xml
<?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"
tools:context="com.example.median1.psalmsmarket.Psalms">
<WebView
android:id="#+id/Pslamsview"
android:layout_width="match_parent"
android:layout_height="1400px" />
<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_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="450dp"></com.google.android.gms.ads.AdView>
</RelativeLayout>
and if you seen when i open this application with size 720*1280
it show in normal way they dont get mixed
but when i open it with a different size that will happens
Do not use fix size. If you are using Relative Layout then this is the best way to ensure the preferred output -
<?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"
tools:context="com.example.median1.psalmsmarket.Psalms">
<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_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"/>
<WebView
android:id="#+id/Pslamsview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/adView"/>
</RelativeLayout>
You can change your layout from Relative to LinearLayout , with linear layout you can use weightSum concept and layout_weight , with this the view will get equally spaced in all screens .
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:weightSum="1"
tools:context="com.example.median1.psalmsmarket.Psalms">
<WebView
android:id="#+id/Pslamsview"
android:layout_width="match_parent"
android:layout_height="0"
android:layout_weight="1" />
<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_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="450dp"></com.google.android.gms.ads.AdView>
</RelativeLayout>
<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"
tools:context="com.example.median1.psalmsmarket.Psalms">
<WebView
android:id="#+id/Pslamsview"
android:layout_width="match_parent"
android:layout_height="match_parent"
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"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="450dp"></com.google.android.gms.ads.AdView>
Change height of your webview to android:layout_height="match_parent" and add this property android:layout_above="#+id/adView"
Remove your hard height 1400px and make it match_parent then set the webview to be above the adView android:layout_above="#+id/adView"
<WebView
android:id="#+id/Pslamsview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/adView" />