How do I programmatically get the current screen background color?

I write a simple program in kotlin. Creating an application that has one screen. The screen color is white, and the text fields have different colors and sizes. There are also 2 buttons on the screen: Refresh and Roll back. When you click on Refresh, the background color changes to blue, and the colors and dimensions of the TextView also change. When you click on Roll back, everything should roll back to its original state (white screen, text fields are the same as when the program starts). Please tell me how it is possible in the code, suppose MainActivity get the background color of the screen (background)? The idea is this: I get the background color at the start of the program, then pass it through SharedPreferences, so that when you click on the Roll back button, set the same background color. the root element of the screen is called root_srl. I tried something like root_srl.getBackground (), but I didn't find anything similar

Author: Эникейщик, 2020-09-30

1 answers

You can get the color of your root View this way:

int color = Color.TRANSPARENT;
Drawable background = view.getBackground();
if (background instanceof ColorDrawable)
    color = ((ColorDrawable) background).getColor();
 1
Author: Sergei Buvaka, 2020-10-01 07:19:43