I'm a beginner on Android Studio and I've searched almost everywhere but haven't found anything working for me.
I want to update the view immediately after changing its background color every time in an infinite loop. I have tried with invalidate() but, after tapping, the view is still white. How can I redraw the view after changing?
public class Main extends AppCompatActivity {
static int r = 0, g = 0, b = 0;
static int current, direction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final View view = findViewById(R.id.textView);
final View root = view.getRootView();
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
while(true) {
rainBow();
root.setBackgroundColor(Color.rgb(r, g, b));
view.invalidate();
root.invalidate();
}
}
});
}
public static void rainBow() {
if(r == 0 && g == 0 && b == 0) {
current = 1;
direction = 1;
} else if(r == 255 && g == 0 && b == 0) {
current = 2;
direction = 1;
} else if(r == 255 && g == 255 && b == 0) {
current = 1;
direction = -1;
} else if(r == 0 && g == 255 && b == 0) {
current = 3;
direction = 1;
} else if(r == 0 && g == 255 && b == 255) {
current = 2;
direction = -1;
} else if(r == 0 && g == 0 && b == 255) {
current = 1;
direction = 1;
} else if(r == 255 && g == 0 && b == 255) {
current = 3;
direction = -1;
}
switch(current) {
case 1:
r += direction;
break;
case 2:
g += direction;
break;
case 3:
b += direction;
break;
}
}
}