Search code examples
androidandroid-layoutandroid-appbarlayout

Aligning Favourite button in AppBar


I create this detail screen

Detail Activity

I want to move app the favorite button like

enter image description here

This is my code.

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/photo_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true"
    >
    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="350dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleTextAppearance="@android:color/transparent"
        android:fitsSystemWindows="true">

        <ImageView
            android:id="@+id/detail_image_view"
            app:layout_collapseMode="parallax"
            android:src="@drawable/ic_broken_image"
            android:contentDescription="@string/app_name"
            android:layout_width="match_parent"
            android:scaleType="centerCrop"
            android:layout_height="350dp" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:title="@string/app_name"
            app:layout_collapseMode="pin"></android.support.v7.widget.Toolbar>
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    >
<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    app:cardElevation="5dp"
    app:cardUseCompatPadding="true">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/detail_title"
            android:padding="12dp"
            android:textSize="20sp"
            android:layout_marginTop="12dp"
            android:textStyle="bold"
            android:textColor="@color/colorPrimaryDark"
            android:text="Recent News"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <LinearLayout
            android:padding="12dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:contentDescription="@string/app_name"
                android:src="@drawable/ic_clock" />

            <TextView
                android:layout_gravity="center_vertical"
                android:id="@+id/detail_publish_date"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="TextView" />


        </LinearLayout>

    </LinearLayout>



</android.support.v7.widget.CardView>
</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fav_fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="@dimen/fab_margin"
    android:layout_marginRight="@dimen/fab_margin"
    android:elevation="@dimen/fab_elevation"
    android:src="@drawable/ic_favorite_black_24dp"
    app:pressedTranslationZ="12dp"
    app:layout_anchor="@+id/app_bar_layout"
    app:layout_anchorGravity="bottom|right|end"
    />

How can I do that?

Thanks,

Theo.


Solution

  • Just Remove android:layout_gravity="bottom|end" from your FloatingActionButton it will work

    SAMPLE CODE

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/photo_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="350dp"
                app:contentScrim="?attr/colorPrimary"
                app:expandedTitleTextAppearance="@android:color/transparent"
                app:layout_collapseParallaxMultiplier="0.7"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                app:title=""
                app:titleEnabled="false">
    
                <ImageView
                    android:id="@+id/detail_image_view"
                    android:layout_width="match_parent"
                    android:layout_height="350dp"
                    android:contentDescription="@string/app_name"
                    android:scaleType="centerCrop"
                    android:src="@drawable/kid_goku"
                    app:layout_collapseMode="parallax" />
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/detail_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:title="@string/app_name" />
    
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                app:cardElevation="5dp"
                app:cardUseCompatPadding="true">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                    <TextView
                        android:id="@+id/detail_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="12dp"
                        android:padding="12dp"
                        android:text="Recent News"
                        android:textColor="@color/colorPrimaryDark"
                        android:textSize="20sp"
                        android:textStyle="bold" />
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:padding="12dp">
    
                        <ImageView
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:contentDescription="@string/app_name"
                            android:src="@drawable/ic_camera" />
    
                        <TextView
                            android:id="@+id/detail_publish_date"
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:layout_weight="1"
                            android:text="TextView" />
    
    
                    </LinearLayout>
    
                </LinearLayout>
    
    
            </android.support.v7.widget.CardView>
    
        </android.support.v4.widget.NestedScrollView>
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fav_fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    
            android:layout_marginBottom="@dimen/fab_margin"
            android:layout_marginRight="@dimen/fab_margin"
            android:elevation="5dp"
            android:src="@drawable/ic_camera"
            app:layout_anchor="@+id/app_bar_layout"
            app:layout_anchorGravity="bottom|end"
            app:pressedTranslationZ="12dp" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    OUTPUT

    enter image description here