Android FrameLayout hosting the viewPager consuming all available space
Hi everyone, I'm trying to create a "tutorial" (like this one from the AirBnb app) with different images and text (both placed inside a Fragment) and controlled by a (Fragment) ViewPager inside a FrameLayout. As an example above, I want to also display two buttons at the bottom of the screen, my problem is that the FrameLayout that contains the ViewPager takes up all the available space, thereby hiding the LinearLayout that contains the buttons.
This is a weird situation, as the layouts are very simple, I tried different ways, but nothing solves my problem. I checked that only the root layout elements were in fill_parent mode, and also checked that all other layouts did not exceed their required space to display their information.
Here are some snippets, xml layouts for the activity and for each child snippet displayed by the FragmentViewPager:
activity-tutorial.xml (this one I tried with LinearLayout as root instead of RelativeLayout but nothing changes)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/pager_framelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/tutorial_pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/circle_indicator"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@null"
android:layout_gravity="bottom" />
</FrameLayout>
<LinearLayout
android:id="@+id/buttons_bottom_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="100"
android:layout_below="@id/pager_framelayout">
<Button
android:id="@+id/sign_up_btn"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="50"
android:text="Sign UP" />
<Button
android:id="@+id/login_btn"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="50"
android:text="Login" />
</LinearLayout>
</RelativeLayout>
fragment_tutorial_screen.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/tutorial_screen_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<TextView
android:id="@+id/tutorial_screen_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/general_padding"
android:background="@null"
android:layout_gravity="bottom"/>
</FrameLayout>
And the java code to instantiate the fragment and populate the views with information:
TutorialScreenFragment.java
{ //...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Identify and set fields!
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.subfragment_tutorial_screen, container, false);
image = (ImageView) rootView.findViewById(R.id.tutorial_screen_image);
title = (TextView) rootView.findViewById(R.id.tutorial_screen_title);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate fields with info!
if (TutorialActivity.class.isInstance(getActivity())) {
title.setText(titleResId);
// Call Glide to load image
Glide.with(this)
.load(imageResId)
.centerCrop()
.placeholder(R.drawable.image_placeholder)
.into(image);
}
}
/...
}
Everything works fine, also the dots that show the current page, but the buttons are not shown at all. This is the output from my application:
Thank.
source to share
You need to change the behavior of the layout. First, align yours LinearLayout
to the bottom of the parent. And then place it FrameLayout
above LinearLayout
. Something like that:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@+id/pager_framelayout"
android:layout_width="fill_parent"
android:layout_above="@+id/buttons_bottom_layout"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="@+id/tutorial_pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<com.viewpagerindicator.CirclePageIndicator
android:id="@+id/circle_indicator"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="@null"
android:layout_gravity="bottom" />
</FrameLayout>
<LinearLayout
android:id="@+id/buttons_bottom_layout"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:weightSum="100"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/sign_up_btn"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="50"
android:text="Sign UP" />
<Button
android:id="@+id/login_btn"
android:padding="@dimen/general_padding"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="50"
android:text="Login" />
</LinearLayout>
</RelativeLayout>
source to share