JerryKo
JerryKo

Reputation: 384

Make a button that displays in all fragment android

I am trying to make a button that lies at the bottom and will show on all the fragments. This is my xml layout for the activity that contains the fragment. Fragments are scrollable and the bottom button is always displayed:

<?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:id="@+id/activity_store_info"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sendbest.ibooking.ibooking_customer.StoreInfo">

<com.ogaclejapan.smarttablayout.SmartTabLayout
    android:id="@+id/viewpagertab"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    app:stl_indicatorAlwaysInCenter="false"
    app:stl_indicatorWithoutPadding="false"
    app:stl_indicatorInFront="false"
    app:stl_indicatorInterpolation="smart"
    app:stl_indicatorGravity="bottom"
    app:stl_indicatorColor="#ff4081"
    app:stl_indicatorThickness="4dp"
    app:stl_indicatorWidth="auto"
    app:stl_indicatorCornerRadius="2dp"
    app:stl_overlineColor="#4D000000"
    app:stl_overlineThickness="0dp"
    app:stl_underlineColor="#4D000000"
    app:stl_underlineThickness="1dp"
    app:stl_dividerColor="#4D000000"
    app:stl_dividerThickness="1dp"
    app:stl_defaultTabBackground="?attr/selectableItemBackground"
    app:stl_defaultTabTextAllCaps="true"
    app:stl_defaultTabTextColor="#4D000000"
    app:stl_defaultTabTextSize="12sp"
    app:stl_defaultTabTextHorizontalPadding="16dp"
    app:stl_defaultTabTextMinWidth="0dp"
    app:stl_distributeEvenly="false"
    app:stl_clickable="true"
    app:stl_titleOffset="24dp"
    app:stl_drawDecorationAfterTab="false"
    />

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/viewpagertab"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_below="@id/viewpager"
    android:gravity="bottom"
    android:text="bottom button"
    android:background="#e50013"
    android:id="@+id/btShopping"
    android:layout_alignParentBottom="true"
    />

</RelativeLayout>

The tab and viewpager works fine now, however the button cannot be displayed. What is the correct way to apply this?

Upvotes: 1

Views: 143

Answers (5)

Sneha Sarkar
Sneha Sarkar

Reputation: 731

Change parent layout which in your case in RelativeLayout to LinearLayout with android:orientation="vertical".

Then in ViewPager change android:layout_height="match_parent" to android:layout_height="0dp" and add another line of android:layout_weight="1".

Hope in this way, you will find the solution.

Upvotes: 1

Avani Paneliya
Avani Paneliya

Reputation: 122

remove this from button

android:layout_below="@id/viewpager"

Upvotes: 1

Thinsky
Thinsky

Reputation: 4246

because you set the viewpager mathc_parent and set button below viewpager, the button out of the screen.

Upvotes: 0

Suresh Kumar
Suresh Kumar

Reputation: 2034

Just remove this line android:layout_below="@id/viewpager" from your button. It will display in all fragment.

Upvotes: 1

Nilesh Singh
Nilesh Singh

Reputation: 1750

add this to your ViewPager (XML):

android:layout_above="@+id/btShopping"

and remove this from the same Button:

android:layout_below="@+id/viewpager"

The issue with your code is that the ViewPager is covering the whole Activity and even if the Button is set to be just below it, it gets out of activity screen.

Upvotes: 0

Related Questions