0

I want to make a loop which changes the color of the ImageView but it isn't working.. In the XML file of the Activity I gave some jpg in the background attribute of ImageView. The colors are from the String Array. Log shows iteration is ok.. It changes ImageView to white (not visible? )

Code:

public class MainActivity extends AppCompatActivity {

    ImageView imV;
    int totalStep = 4;
    int step = 0;
    private Handler handler;
    String colors[] = {"#000000", "#ff4455", "#ff1133", "#ff0000", "#00ffff"};

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

        imV = (ImageView) findViewById(R.id.imageView);

        handler = new Handler();
        handler.postDelayed(runnable, 1000);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (step == totalStep) {
                step = 0;
            } else {
                step++;
            }
            imV.setBackgroundColor(Color.parseColor(colors[step]));
            Log.d("Tick", " " + step);
            Log.d("color", " _ " + colors[step]);

            handler.postDelayed(this, 1000);
        }
    };
}
Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
baribari
  • 31
  • 4
  • 2
    "It changes ImageView to white ( not visible ? )" - If you mean, it just kinda disappears, I would guess that you have its width/height set to `wrap_content`, which will collapse it to 0x0 after replacing the image you had as the background with a color, which doesn't have any intrinsic dimensions. You can test it by setting exact dimensions. – Mike M. Oct 12 '16 at 23:54
  • Yes! Thanks! Should I add wrap_content everytime or use once defined constant values? – baribari Oct 13 '16 at 00:01
  • 1
    It depends on what you want. Using `wrap_content` is perfectly fine, but if it doesn't have any content, it'll shrink to 0, as you've seen. If you want your `ImageView` to keep a certain size no matter what, you'll wanna set definite dimensions on it, either in the layout XML, or in code. I would also mention that if you're trying to change the background with the image still in place, you want to set the image as the `src` attribute, not the `background` attribute. – Mike M. Oct 13 '16 at 00:15
  • Use a countDownTimer – Zar E Ahmer Oct 13 '16 at 05:41
  • Or better use Animation for doing this http://stackoverflow.com/a/34429554/3496570 or http://stackoverflow.com/questions/18216285/android-animate-color-change-from-color-to-color – Zar E Ahmer Oct 13 '16 at 05:46

1 Answers1

0

// declare it on global

Timer t = new Timer();

// write this code in onCreate() or as per your need

imV = (ImageView) findViewById(R.id.imageView);

    t.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        if (step == totalStep) {
                            step = 0;
                        } else {
                            step++;
                        }
                        imV.setBackgroundColor(Color.parseColor(colors[step]));
                        Log.e("Tick", " " + step);
                        Log.e("color", " _ " + colors[step]);
                    }
                });
        }
    }, 0, 1000);

//

make sure you have given some height width to your ImageView else its shows blank

Rajesh Satvara
  • 3,842
  • 2
  • 30
  • 50