fgoogle fgoogle - 2 months ago 20
Android Question

How to use a resource color id with .setBackgroundDrawable

I am trying to color my toolbar and status bar with two colors I have defined in the colors.xml file.

((AppCompatActivity) getActivity())
.getSupportActionBar()
.setBackgroundDrawable(?);


window.setStatusBarColor(?);


What should be put inside the brackets in order to refer to
R.color.a
and
R.color.b
?

Answer

Pass RGB value of the color

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.rgb(248, 248, 248)));

To pass Resourse id

getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(getContext(),R.color.primary)));