i want to change image of ImageView inside of a DialogFragment every 1s ; the code is running ok in android 5 or 4 . but in android 2.3.7 (api 10) it changes ui for once and then doesnt change ui anymore .meanwhile thread is running okay so far , i tried handler message method with a Timer , handler normal method , runOnUIThread . View.Post method but no luck
Bitmap[] onItems = new Bitmap[3];
Bitmap[] offItems= new Bitmap[3];
ImageView[] cheatHoldrs = new ImageView[3];
public Dialog onCreateDialog(Bundle savedInstanceState) {
//all Bitmaps and ImageView has been inited
myMethod();
}
View.Post Method :
for (int i = 0; i < 3; i++) {
final int j = i;
cheatHoldrs[j].post(new Runnable() {
@Override
public void run() {
random Random = new Random();
cheatHoldrs[j].setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
cheatHoldrs[j].postDelayed(this, 1000);
}
});
}
RunOnUIThread
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Random random = new Random();
for (int j = 0; j < 3; j++)
cheatHoldrs[j]
.setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
new Handler().postDelayed(this, 1000);
}
});
Noraml Handler method :
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
Random random = new Random();
for (int j = 0; j < 3; j++)
cheatHoldrs[j]
.setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
handler.postDelayed(this, 1000);
}
});
Handler Message Method :{ in global scope :
public Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
Random random = new Random();
for (int j = 0; j < 3; j++)
cheatHoldrs[j]
.setImageBitmap((random.nextInt(3) % 3 == 1) ? onItems[j]
: offItems[j]);
}
};
in OnCreateDialog:
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.obtainMessage(1).sendToTarget();
}
}, 0, 1000);
}
EDIT: i finally know whats the problem is . threads are working fine and if i dont use animation, ui updates . when dialog starts showing . i start an animation and move ImageViews (cheatHoldrs) . after the animation ImageViews image does not changes . i tried 9oldroid animation and TranslateAnimation like this but still dosent change ui after animation : for api > 11 i use ObjectAnimator and it works fine , but for api < 11 i use this methods
TranslateAnimation animation = new TranslateAnimation(0, forLeft, 0, 0);
animation.setFillAfter(true);
animation.setDuration(1000);
cheatHoldrs[1].startAnimation(animation);
TranslateAnimation animation2 = new TranslateAnimation(0, forLeft, 0, 0);
animation2.setFillAfter(true);
animation2.setDuration(1000);
cheatHoldrs[2].startAnimation(animation2);
TranslateAnimation animation3 = new TranslateAnimation(0, forRight, 0, 0);
animation3.setFillAfter(true);
animation3.setDuration(1000);
cheatHoldrs[0].startAnimation(animation3);
and nineoldandroids method :
com.nineoldandroids.animation.ObjectAnimator transitionForFirst = com.nineoldandroids.animation.ObjectAnimator
.ofFloat(cheatHoldrs[0], "translationX", forRight);
transitionForFirst.setDuration(1000);
com.nineoldandroids.animation.ObjectAnimator transitionForSecound = com.nineoldandroids.animation.ObjectAnimator
.ofFloat(cheatHoldrs[1], "translationX", forLeft);
transitionForSecound.setDuration(1000);
com.nineoldandroids.animation.ObjectAnimator transitionForThird = com.nineoldandroids.animation.ObjectAnimator
.ofFloat(che
atHoldrs[2], "translationX", forLeft);
transitionForThird.setDuration(1000);
transitionForFirst.start();
transitionForSecound.start();
transitionForThird.start();