Omega
Omega

Reputation: 299

How to add white background to a float action button

I have a float action button which i implemented on my MainActivity, am still a rookie and what i intend to achieve is to add a white background to this float action button when the button is clicked. This is the code am currently using, i will appreciate an edit to my code when answering:-

 final ImageView fabIconNew = new ImageView(this);
      fabIconNew.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_new));
    final FloatingActionButton rightLowerButton = new FloatingActionButton.Builder(this)
            .setContentView(fabIconNew)
            .build();


    SubActionButton.Builder rLSubBuilder = new SubActionButton.Builder(this);
    ImageView rlIcon1 = new ImageView(this);
    ImageView rlIcon2 = new ImageView(this);
    ImageView rlIcon3 = new ImageView(this);
    ImageView rlIcon4 = new ImageView(this);
    //ImageView rlIcon6 = new ImageView(this);


    rlIcon1.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_contact));
    rlIcon2.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_currency_info));
    rlIcon3.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_exhibition));
    rlIcon4.setImageDrawable(getResources().getDrawable(R.drawable.ic_action_faq));

Upvotes: 1

Views: 397

Answers (3)

Ichor de Dionysos
Ichor de Dionysos

Reputation: 1137

You can change the Color in Java using:

rightLowerButton.setBackgroundColor(context.getColor(R.color.your_color));

Upvotes: 0

vguzzi
vguzzi

Reputation: 2428

FAB's use the colorAccent attribute to get their colour. Change this in your theme to whatever colour you want it to be.

<item name="colorAccent">#FFFFFF</item>

Or change the background colour at run time like so:

rightLowerButton.setBackgroundColor(getActivity().getResources().getColor(R.color.white));

Upvotes: 1

Sebastian Schneider
Sebastian Schneider

Reputation: 5532

It's possible to change the color like this:

Java:

rightLowerButton.setBackgroundColor(R.color.colorname);

XML (layout/colors.xml):

<xml>
   <color name="colorname">#FFFFFF</color>
</xml>

Upvotes: 0

Related Questions