Change image Floating Action Button Android

前端 未结 9 454
旧时难觅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:52

    I had the same problem and I managed to create my own solution. Maybe some else founds it also useful. I have posted the complete answer to another question (How to set an icon to getbase FloatingActionsMenu) but this part posted here is relevant to the question in dynamically changing the main menu button picture/image when one of the sub buttons is chosen. In this case, you need o combine the answer from the "linked question" and the answer below.

    In order to change the icon on the menu button when you choose a floatingActionButton it can be implemented like this:

    Create menu button in xml file, create floating button(s) on .java file (programmatically) set menu button (color button, color pressed button and image). Then simply add all buttons to the menu button. You can also deactivate the animation of the menu button by simply commenting out the code in FloatingActionsMenu class.

    Then every time that you create a button, sample:

    final FloatingActionButton actionA = new FloatingActionButton(getBaseContext());
            actionA.setTitle("Familie");
            actionA.setIcon(R.drawable.world_map);
            actionA.setSize(FloatingActionButton.SIZE_MINI);
            actionA.setColorNormalResId(R.color.red);
            actionA.setColorPressedResId(R.color.black_semi_transparent);
            actionA.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    menuMultipleActions.setMenuButton(R.drawable.icon, R.color.red_transparent, R.color.black_semi_transparent);
                    Toast.makeText(MainMapView.this, "Action Description", Toast.LENGTH_SHORT).show();
                    ((FloatingActionsMenu) findViewById(R.id.multiple_actions)).collapse();
                    return;
                }
            });
    

    See the answer posted on the link on how to configure the classes and define the menu button and floating button(s).

    So the important part to notice here is:

    menuMultipleActions.setMenuButton(R.drawable.icon, R.color.red_transparent, R.color.black_semi_transparent);
    

    This method you need to add in the FloatingActionsMenu class. Simply you call the method after on every floatingActionButton you want to update the image.

    More information you can find on the link that I have posted. So when you click one of the floatingActionButton(s).

    For the moment the color on the menu button is not updating correctly but I am working on it if I find a solution I will update the answer here as well. Hope this helps, happy coding.

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

    What I am using as follows,

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_btn);
    
    // State 1 -on
    
    fab.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.fab_on));
    
    // State  2 - off
    
    fab.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.fab_off));
    

    Hope this will help you

    0 讨论(0)
  • 2020-12-16 10:02

    in kotlin, this will be

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    private fun pauseTimer() {
    floatingActionButton.setImageDrawable(getDrawable(R.drawable.ic_baseline_play_arrow))
    

    happy coding, Nenad

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