How to place the Floating Action Button exclusively on one fragment only in Tab layout

20,663

Solution 1

Here is what I did for a similar kind of problem. I declared a coordinator layout for the fragment. Inside that coordinator layout as you can see in the image i have declared a recyclerView that I use and below that i declared my floating action button.

After this I inflated the layout for floating action button in the onCreateView fragment method.

Here is my XML File Code:

<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
    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:focusableInTouchMode="true">

    <EditText
        android:id="@+id/search_string"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_margin="5dp"
        android:layout_marginTop="50dp"
        android:hint="Search Keyword"
        android:drawableLeft="@drawable/search"
        android:gravity="left" >
    </EditText>

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/photo_recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="50dp"
    android:scrollbars="vertical"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_action_name6" />
</android.support.design.widget.CoordinatorLayout>

Here is my Java File Code:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_photo_list, container, false);

        FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Photo photo = new Photo();
                PhotoLab.get(getActivity()).addPhoto(photo);
                Intent intent = PhotoPagerActivity.newIntent(getActivity(), photo.getId());
                startActivity(intent);
                /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();*/
                }
        });

        mSearchString=(EditText) view.findViewById(R.id.search_string);
        mPhotoRecyclerView = (RecyclerView) view.findViewById(R.id.photo_recycler_view);
        mPhotoRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));

        updateUI();
        return view;
    }

Solution 2

You might check out my answer here: FAB animation with viewpager/tabslider

I think that might be roughly what you're looking for, but you will have to use the Support Library's FAB to accomplish it.

Solution 3

in your firstfragment.xml you must define the floating action button using the above library. i think u r defining it in you activity which has two fragments.

Solution 4

It seems like this library assumes that you want to attach the FAB to your activity. The layout params that it generates also seems to assume that the parent is a FrameLayout. It is a little bit hacky, but you can detach it and reattach it where you want after you build() it.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    FrameLayout root = (FrameLayout) inflater.inflate(R.layout.fragment_fab, container, false);

    FloatingActionButton actionButton = new FloatingActionButton.Builder(getActivity())
            .build();

    actionButton.detach();
    root.addView(actionButton);

    return root;
}
Share:
20,663
Steve Kamau
Author by

Steve Kamau

I am an enthusiast in android development although i do not have experience in java coding,am learning from the tuitorials all over google.I extract sample projects and piecing different codes to make apps for learning.

Updated on July 09, 2022

Comments

  • Steve Kamau
    Steve Kamau almost 2 years

    I am trying to achieve the following with no success.I have two material design swipe tabs.I used this library 'com.oguzdev:CircularFloatingActionMenu:1.0.2' to achieve FAB but when i swipe the tab to the second tab,the FAB is still attached to the new fragment.

    I would like to hide the FAB on the second Fragment. Is there a way to achieve this?