简体   繁体   中英

How to implement Cast button as Floating Action Button?

I'm trying to implement Cast button as a floating action button but I don't know how.

I've got it working on the toolbar without any issues so far but I wanted to try it doing as a floating action button.

I've tried the following but it doesn't work like the real floating action button.

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways|snap" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:contentInsetStart="0dp"
        android:contentInsetLeft="0dp"
        android:contentInsetEnd="0dp"
        android:contentInsetRight="0dp"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp"
        app:hideOnScroll="true"
        app:layout_scrollFlags="scroll|enterAlways|snap">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="5">

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/ib_monitors"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:tint="@color/colorAccent"
                app:srcCompat="@drawable/ic_photo_camera_black_24dp" />

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/ib_videos"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:tint="@color/colorAccent"
                app:srcCompat="@drawable/ic_video_library_black_24dp" />

            <androidx.appcompat.widget.AppCompatImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless" />

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/ib_settings"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:tint="@color/colorAccent"
                app:srcCompat="@drawable/ic_settings_black_24dp" />

            <androidx.appcompat.widget.AppCompatImageButton
                android:id="@+id/ib_account"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:tint="@color/colorAccent"
                app:srcCompat="@drawable/ic_account_circle_black_24dp" />

        </LinearLayout>

    </com.google.android.material.bottomappbar.BottomAppBar>

    <androidx.mediarouter.app.MediaRouteButton
        android:id="@+id/media_route_button"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:backgroundTint="@color/fabBackgroundTint"
        app:layout_anchor="@id/bar"
        app:tint="@color/colorAccent"/>

    <!--<com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:backgroundTint="@color/fabBackgroundTint"
        app:layout_anchor="@id/bar"
        app:srcCompat="@drawable/ic_add_a_photo_black_24dp"
        app:tint="@color/colorAccent" />-->

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Appreciate any tips.

Add the 2 views (FAB and media route button) in a RelativeLayout. Align both views to center in parent. And finally add "elevation" property so Media Route Button shows on top of FAB:

   <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/viewpager"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="15dp">

        <android.support.design.widget.FloatingActionButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true" />

        <android.support.v7.app.MediaRouteButton
            android:id="@+id/home_media_route_button"
            android:layout_width="wrap_content"
            android:elevation="7dp"
            android:layout_height="wrap_content"
            android:mediaRouteTypes="user"
            android:visibility="visible"
            android:layout_centerInParent="true"/>
    </RelativeLayout>

This is the implementation I have. It works for me, I hope it works also for you

1.- First thing, you need to add the MediaRouteButton

<androidx.mediarouter.app.MediaRouteButton
        android:id="@+id/home_media_route_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:mediaRouteTypes="user"
        android:visibility="visible"/>

2.- Now you just need to add the floating button style to your button

style="@style/Widget.Design.FloatingActionButton"

3.-As final step you only need to setup your MediaRouteButton like this

 CastButtonFactory.setUpMediaRouteButton(this,home_media_route_button)

BONUS

If you want the button to display only when cast devices are available then add this code to your onCreate method

private lateinit var castContext: CastContext

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        castContext = CastContext.getSharedInstance(this)
        castContext.addCastStateListener{state->
            if(state==CastState.NO_DEVICES_AVAILABLE) {
                home_media_route_button.visibility = View.GONE
            }else{
                home_media_route_button.visibility=View.VISIBLE
            }
        }
        if(castContext.castState==CastState.NO_DEVICES_AVAILABLE){
            home_media_route_button.visibility = View.GONE
        }else{
            home_media_route_button.visibility=View.VISIBLE
        }
        CastButtonFactory.setUpMediaRouteButton(this,home_media_route_button)
}

Let me know if it works for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM