Floatingactionbutton does not work

Asked

Viewed 272 times

0

My Floatingactionbutton does not work. In my application I have a Fragment and two tabs. In one of the tabs, floatingActionButton works, in the other not. This is the layout of the main Fragment, where the button is located:

<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="@color/actionbar"/>

<!-- The main content view -->
<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/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".WorkRequestOpenTabs">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            app:layout_scrollFlags="scroll|enterAlways">

        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabSelectedTextColor="@color/white"
            app:tabMode="scrollable"
            app:tabGravity="fill"/>

        <io.codetail.widget.RevealFrameLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <Button android:id="@+id/btnTabAction"
                android:layout_height="0dp"
                android:layout_width="fill_parent"
                android:background="@color/colorAccent"
                android:text="@string/EndLabor"
                android:layout_alignParentBottom="true"
                android:visibility="invisible"/>

        </io.codetail.widget.RevealFrameLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appbar"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

</RelativeLayout>

This is the code of the Tab (photos), where I need to adjust an action to the button:

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    Log.i(this.getClass().getCanonicalName(), "###### onCreateView");
    rootView = inflater.inflate(R.layout.tabworkrequestdocuments, container, false);

    FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab);
    fab.bringToFront();

    if(fab != null) {

        fab.setImageResource(R.drawable.add_sign);
        fab.setVisibility(View.VISIBLE);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addDocument();
            }
        });
    }

    return rootView;
}
  • 1

    If the FAB will have different functionality in each tab, why not implement one in each Fragment? I’m not seeing gain in reuse the same FAB and change in real time the icon, method that will be used in the click, etc when changing tab.

  • I understood, I hadn’t thought of it that way. I have this implementation in another project, and it works all right there. I will try to apply 1 in each and notice here if it worked. Vlw Márcio!

1 answer

2


Really, I separated the Floatingactionbutton between the TABS and everything worked out.

<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<ListView android:id="@+id/listviewPictures"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:layout_alignParentLeft="true">
</ListView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

</RelativeLayout>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.