Change image Floating Action Button Android

前端 未结 9 456
旧时难觅i
旧时难觅i 2020-12-16 09:19

I used this library https://github.com/futuresimple/android-floating-action-button. How can I change the image of the main button? I want to change the button image right af

相关标签:
9条回答
  • 2020-12-16 09:36

    From https://developer.android.com/reference/android/support/design/widget/FloatingActionButton.html

    As this class descends from ImageView, you can control the icon which is displayed via setImageDrawable(Drawable).

    Or you can use setImageResource():

    fab.setImageResource(R.drawable.ic_shopping_cart_white);
    
    0 讨论(0)
  • 2020-12-16 09:37

    Add the property

    fab:fab_icon="@drawable/fab_expand"
    

    in the xml where you initialized the floating action menu.

    0 讨论(0)
  • 2020-12-16 09:39

    I faced same issue recently, I tried with following option

      fab:fab_icon="@drawable/icon"
    

    and

     android:src="@drawable/icon" // change backgroung icon
    

    even tried programmatically

     fab_menu_btn.setImageResource();
    

    Nothing worked.

    Solution: In app's build.gradle file replace

      compile 'com.getbase:floatingactionbutton:1.10.0'
    

    to

    compile 'com.github.toanvc:floatingactionmenu:0.8.9'
    

    In .xml file Use:

               <toan.android.floatingactionmenu.FloatingActionsMenu
              </toan.android.floatingactionmenu.FloatingActionsMenu>
    

    In activity File:

          floatingActionsMenu.setIcon(getResources().getDrawable(R.mipmap.icon));
    

    Thanks

    0 讨论(0)
  • 2020-12-16 09:39

    In the Material design version:

    • build.gradle (app)
    defaultConfig {
        vectorDrawables.useSupportLibrary = true // For srcCompat
    }
    
    dependencies {
        implementation 'com.google.android.material:material:<version>'
    }
    
    • XML (for set icon)
    app:srcCompat="@drawable/ic_google"
    app:tint="@color/colorGoogle"
    

    More documentation: https://material.io/develop/android/components/floating-action-button/

    0 讨论(0)
  • 2020-12-16 09:40

    you can use this in your .XML :

    android:src="@drawable/icon" // change backgroung icon
    app:backgroundTint="@color/icons" // change background color
    

    use this in code:

     mFloatingActionButton.setImageResource(R.drawable/icon2);
    
    0 讨论(0)
  • 2020-12-16 09:40

    Unfortunately with this library you can't change the icon from the menu (see issues from this library from more info)

    That is why I dropped this library to use a way more flexible one! It is originally a fork but it is now more advanced ;)

    Here is the link

    Enjoy!

    0 讨论(0)
提交回复
热议问题