Fatima
Fatima

Reputation: 869

How to force FAB go to the bottom of the layout?

My first time using FAB. NO matter what setting I use for gravity and anchor attributes, the FAB always appears on top. I want to make it appear at the bottom right corner. How am I supposed to achieve that?

My layout xml

    <?xml version="1.0" encoding="utf-8"?><!-- DO NOT EVER CHANGE THIS LINEAR LAYOUT TO ANYTHING ELSE WITHOUT CHECKING PropertyDetailFragment.fillYourself cause it's used there to add children to it-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/content"
              style="@style/StyleForVerticalLinearLayoutNoPadding"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
        >
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="true"
        android:src="@android:drawable/ic_dialog_map"
        app:layout_anchorGravity="bottom|right|end"
        android:layout_gravity="bottom|end"
        />

I add some views to this layout in code. But they have their gravity set to top.

Here is how it looks:

enter image description here

Fragment Code:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (savedInstanceState != null && savedInstanceState.containsKey("propertyID"))
        propertyID = savedInstanceState.getLong("propertyID");
    currentPhotoPosition = getActivity().getIntent().getExtras().getInt("currentPhotoPosition");
    final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_property_detail, container, false);
    new PropertyDetailProxy().run(new Response.Listener<PropertyDetailed>() {
        @Override
        public void onResponse(PropertyDetailed propertyDetailed) {
            if (propertyDetailed.getNumberOfPhotos() > 0)
                new PhotosIDsProxy().run(new Response.Listener<long[]>() {
                    private long[] imagesIDs;

                    @Override
                    public void onResponse(long[] photoIDs) {
                        imagesIDs = photoIDs;
                        if (imagesIDs != null && imagesIDs.length > 0) {
                            PhotoAdapter adapter = new PhotoAdapter(getActivity());
                            ViewPager viewPager = new ViewPager(rootView.getContext());
                            rootView.addView(viewPager);
                            viewPager.setAdapter(adapter);
                            adapter.setPropertyImagesIDs(photoIDs);
                            adapter.notifyDataSetChanged();
                            viewPager.setCurrentItem(currentPhotoPosition);
                        }
                    }
                }, new DefaultErrorListener(), propertyID);
            rootView.addView(new PropertyDetailedPersianStringMaker(propertyDetailed).make());
        }
    }, new DefaultErrorListener(), propertyID);
    return rootView;
}

EDIT: Changed code:

      <?xml version="1.0" encoding="utf-8"?>
   <android.support.design.widget.CoordinatorLayout 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">

<RelativeLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
        android:layout_margin="10dp"
        android:id="@+id/myFAB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="#FF0000"
        app:borderWidth="0dp"
        app:elevation="8dp"
        app:layout_anchor="@id/content"
        app:layout_anchorGravity="bottom|right|end" />

result: enter image description here

EDIT 3: enter image description here

Upvotes: 1

Views: 490

Answers (1)

Eagle11
Eagle11

Reputation: 661

A linear layout will always layout the views starting from the top or the left depending on it's orientation. You need to change the layout type to be a relative layout or a CoordinatorLayout so that you can position it how you want.

Upvotes: 1

Related Questions