How to set a fixed background in ViewPager - android

I have a Fragment that contains a ViewPager which allows the user to swipe through three fragments.
Here's the problem: I want the ViewPager to have a background that remains fixed when the user swipes through the fragments. I tried setting an image in my drawable as the background of the ViewPager but that didn't work.. Here's my code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue"
tools:context=".fragments.cash.WalletFragment">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:background="#drawable/revenue"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>

Although there are many ways to achieve this feat, this approach works for me:
Simply create a FrameLayout with two children: Your ViewPager and the view(s) you want to use as the background of your viewpager. Here's an example:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue"
tools:context=".fragments.cash.WalletFragment">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/my_smiling_face"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</RelativeLayout>
Note that you can replace the Imageview with a layout (e.g RelativeLayout) containing all the composite views you want your background to contain.
I hope this helps.. Merry coding!

Related

How make put ViewPager inside ScrollView and scroll all view as one part?

I have an activity that have viewpager inside scrollview like the follwoing
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--someViews-->
</LinearLayout>
<!--then viewPager-->
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
Then the view pager holds some fragments which have a tall view too.
The problem is the scroll view not scrolling because of the view is not need to scroll but the view inside the viewpager fragment's not scrolling
so, Is it possible to make the whole view including the inside view pager scroll as one view?
What I am saying that I don't need to scroll the viewpager fragment as separated part I need to scroll it as part of activity view.
Thanks
I believe the setup you are looking for would work inside a CoordinatorLayout using the NestedScrollView widget like so:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Display1" />
<TextView
android:id="#+id/subTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/subtitle"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Body1" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
I have just tested this with a sample project with a single text view in a Fragment and I was able to both scroll the view and swipe through the pages.
Please note though: I did have to provide a specific height for the ViewPager in order for it to display.

Scrollview doesnt work with framelayout

I have an activity layout that includes my fragment container this way,
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/background_container" />
and my fragment container is compose of linearlayout and a framelayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:padding="10dp">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" />
</LinearLayout>
The problem is that I have to add my scrollable fragment in this container but the scroll doesnt seem to work. My layout goes like this
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
...
...
</LinearLayout>
</ScrollView>
I have tried several answer like
Using NestedScrollView
Adding minimum height to framelayout
Using fillViewport="true"
etc.
But nothing seems to work. Any idea what I've been doing wrong? Any help would be appreciated. Thanks in advance! Cheers!
Edit:
My activity layout contains navigation drawer, i dont know if it matter. And i add the view using
ft.replace(R.id.fragment_container, myFragment).commit();
i also dont know if fragment replace messes with my view. Please help. Thanks
it seems my fragment container's LinearLayout is missing orientation and also frameLayout
android:layout_height should be match_parent
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical" // orientation
android:padding="10dp">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" // match_parent
android:background="#android:color/transparent" />
</LinearLayout>

I am not able to use viewpager inside scroll-view and tab layout without toolbar layout?

I want to use
<android.support.v4.widget.NestedScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/action_bar"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:visibility="gone">
<RelativeLayout
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="220dp">
<ImageView
android:id="#+id/cover_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/image"
android:background="#color/card_background">
<android.support.design.widget.TabLayout
android:id="#+id/topTabs"
style="#style/TextAppearance.Tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="#color/bpWhite"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextColor="#color/secondary_text_color"
custom:tabIndicatorColor="#color/secondary_text_color"
custom:tabIndicatorHeight="2dp" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_below="#id/topTabs"
android:background="#drawable/tab_layout_drop_shadow" />
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/tab_layout" />
</RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
It is not able to scroll, that view-pager or tab-layout is not able scroll I tried all the ways.
1.Disable the touch event in view-pager and enable the touch event for scroll-view.
2.Tried other scroll-views like Nestedscrollview, Parallaxscrollview, Coordinator layout.If I use coordinator layout then not able to change he toolbar.
I used fillviewport true for all the scrollviews. but still it is not able to scroll.
Any Other ways to fix the issue.
You can use view pager without the toolbar, but you can't use the tabs without the toolbar.
Although it remains unclear, what you are trying to achieve here, there is no problem in the layout, really.
If you set up the Design Support Library correctly and
if you did not mess up your listeners with your attempts to disable some touch events and
if you take out the tools:visibility="gone" attribute (in order to show anything that can be scrolled) and
if you decide on a specific height (or wrap_content) for the ViewPager and
optionally if you take out the first RelativeLayouts which is unnecessary,
then you should be able to scroll. At least I am inside the layout editor of Android Studio.

Collapsable toolbar with ImageView inside ViewPager fragments

I have an Activity with following XML
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<fragment android:name=".GalleryFragment"
android:id="#+id/gallery_fragment"
android:layout_width="match_parent"
android:layout_height="250dp"
android:transitionName="#string/transition">
...
</LinearLayout>
GalleryFragment with following XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:transitionName="#string/transition">
<android.support.v4.view.ViewPager
android:id="#+id/gallery_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
and ViewPager is hosting a couple (max 20) ImageFragments with following XML
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"/>
</LinearLayout>
Now I would like to add collapsable toolbar with parallax effect in such a way that ImageView from ImageFragment is the one which fades out as it collapses.
I have done this couple of times on much more simpler screens, I had only activity with toolbar and ImageView in it so the process was straightforward. But in this case I have no idea if this is even possible to implement because ImageView is inside Fragment which is inside ViewPager.
Does anyone know if this is even possible to implement and if so can you give me some tips.
Thanks is advance.

TabLayout goes under the content of Framelayout

I have a FrameLayout that includes a TabLayout and a ViewPager inside it. Obviously the ViewPager loads a fragment. Now the problem is that TabLayout goes under the content of fragment when it's loaded and therefore disappears.
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</FrameLayout>
How do I fix it?
EDIT:
using the following code I was able to make it work but now the tabs are always stuck to the top when scrolling... anyway to fix that?
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
Change to a vertical LinearLayout. Then the ViewPager will appear beneath the TabLayout instead of drawing over it.
FrameLayout actually stacks all of its children on top of each other. So in your case this isn't a desired behavior. You should consider using a vertical LinearLayout or a RelativeLayout.
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical" >
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
You should use a LinearLayout with orientation of 'vertical' instead of a FrameLayout. A FrameLayout is generally used when you want to contain a single child (such as a fragment).

Categories

Resources