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.
Related
I have an issue with FragmentContainerView, it looks like this:
<androidx.fragment.app.FragmentContainerView
android:id="#+id/myFragment"
android:name="com.me.MyFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
And in its fragment, I have a recycler view, and a button:
<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">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<Button
android:id="#+id/myBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerInParent="true"
android:gravity="bottom|center" />
However, when I am scrolling, button is also moving up, even though I set up alignParentBottom. I believe the issue is in this line of code:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
which is actually:
<string name="appbar_scrolling_view_behavior" translatable="false">com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior</string>
FragmentContainerView is used in CoordinatorLayout. Note that button is out of SwipeRefreshLayout, so it shouldn't be affected by scrolling the recycler view.
I want to keep it sticky, so it doesn't move when I scroll the screen.
Any ideas?
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'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 want to create a layout that has for example the main layout is a vertical LinearLayout, inside it is a TextView and a wrapped RecyclerView. I want to add a SwipeRefreshLayout that only can be swiped outside the RecyclerView.
Here is the simple XML for this layout:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:padding="40dp"
tools:context=".fragments.warehouse.WHTabOne">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Where should I put the SwipeRefreshLayout to make it only works for outside the RecyclerView?
Make SwipeRefreshLayout your root layout and put everything inside it. Example:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 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:id="#+id/swipe_refresh_layout"
android:padding="40dp"
tools:context=".fragments.warehouse.WHTabOne">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAlignment="center"
android:textSize="18sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
What I was asking for is to prevent the SwipeRefreshLayout from starting when swiping on RecyclerView only, and swiping everywhere else activate SwipeRefreshLayout.
But I found that the RecyclerView is by default overrides SwipeRefreshLayout until it reaches the top of the RecyclerView then it start to activate SwipeRefreshLayout. I thought it won't work because my list was too short and could't notice that. Now that I've added more items to the list, it's clear. So basically, adding everything under SwipeRefreshLayout is the right thing by default it will make it in right way.
I have a layout which is have title textview and recylerview below it.
It all works good. But I am trying to make the whole thing scrollable. Meaning that if I scroll down the rcylerview when new items are added, I want the title textview to scroll up (disappears) as if it is part of the recylerview
Here is my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:text="Near me"
android:textColor="#color/White"
android:textSize="21sp"
android:textStyle="bold" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/swipeContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvVideos"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
I know that I can fake it so item 1 of the recylerview has a different layout which is the title text but I am hoping there is a better option so I don't have to deal with edge cases of number of items and manipulating positions
you need to wrap both the textview and recycler view in a NestedScrollView
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Textview"
android:layout_marginTop="10dp"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.design.widget.CoordinatorLayout>
I hope this helps...Cheers