Change the FAB color via code (programmatically)

1

I'm developing a custom%%, where the colors of the screen elements will vary depending on their properties.

Example: If the type property of the Message object displayed is Activity , then the following elements of the "Azul" file will be used:

    <item name="azulFundo" type="color">#979dfd</item>
    <item name="azulTexto" type="color">#0011ff</item>
    <item name="azulFundoFab" type="color">#343b93</item>

For this, I do the following in the colors.xml method:

          @Override
         protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_mensagem_view); 

           mView = RelativeLayout.class.cast(findViewById(R.id.activity_mensagem_view)); 
           compartilharAction = FloatingActionButton.class.cast(findViewById(R.id.compartilharAction));
           imagemTipo = ImageView.class.cast(findViewById(R.id.imagemTipo));
           textMensagem = TextView.class.cast(findViewById(R.id.textMensagem ));
           final String tipo = getIntent().getStringExtra(MESSAGE_TYPE);

          if("Azul".equals(tipo)){ 
              mView.setBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.azulFundo)); 
              imageType.setImageDrawable(ContextCompat.getDrawable(getBaseContext(), R.drawable.azulImagem));  
              textMessage.setTextColor(ContextCompat.getColor( getBaseContext(), R.color.colorAguaTxt) ); 
              compartilharAction.setBackgroundResource(R.color.azulTexto);

          }else...


      }

I tried using the setBackgroundResource , but it did not work!

Any tips?

    
asked by anonymous 01.09.2017 / 05:44

1 answer

3

From documentation :

  

The background color of this view defaults to your theme's   colorAccent. If you wish to change this at runtime then you can do so   via setBackgroundTintList (ColorStateList).

Direct translation:

The background color of this default this view is the colorAccent of your theme. If you want to change this at runtime then you can do so via setBackgroundTintList ( ColorStateList ).

Then see how it can be done:

compartilharAction.setBackgroundTintList(ColorStateList.valueOf(
    ContextCompat.getColor(getBaseContext(), R.color.azulFundo)));
    
01.09.2017 / 05:59