1

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));
Omega
  • 289
  • 1
  • 4
  • 14

3 Answers3

1

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));
vguzzi
  • 2,420
  • 2
  • 15
  • 19
  • am achieving this float button in java, and the white background is suppose to appear when the button is clicked – Omega Oct 21 '15 at 11:21
  • am getting an error which says: cannot resolve method getActivity() – Omega Oct 21 '15 at 11:37
0

You can change the Color in Java using:

rightLowerButton.setBackgroundColor(context.getColor(R.color.your_color));
Ichor de Dionysos
  • 1,107
  • 1
  • 8
  • 30
  • am getting an error on this line which says: should pass resolved colour instead of resource id here .getResources().getColor(R.color.white) – Omega Oct 21 '15 at 11:36
0

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>
Sebastian Schneider
  • 4,896
  • 3
  • 20
  • 47
  • am getting an error which says: should pass resolved colour instead of resource id here .getResources().getColor(R.color.white) – Omega Oct 21 '15 at 11:36