Android Timer changing interval - android

I want to make a timer that changes a picture on the screen when the interval of the picture is over.
for(int x = 0; x < intervals.size(); x++)
{
t.schedule(new TimerTask(){
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
ImageView foto = (ImageView) findViewById(R.id.imageView1);
Bitmap bmp = sponsorlijst.get(index).getFoto();
foto.setImageBitmap(bmp);
if(index < sponsorlijst.size() -1)
{
index ++;
}
else
{
index = 0;
}
}
}
);
};}, 0, intervals.get(x));
Intervals are a list with the different displaytime of the pictures and the pictures are in sponsorlijst.
Does anyone know how to change the interval of the timer while running? Thanks

Related

Android How to play audio for 2 times before moving to the next one

I want to play the audio two times before playing the next item.
This is my class where I want to display the items.
public void startSlides() {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
Intent intent = getIntent();
int setId = intent.getIntExtra("SelectedSetId",0);
databaseAccess.open();
setId = setId + 1;
List<byte[]> audio = databaseAccess.getAudioA(setId);
if (i > audio.size()){
i = 0;
}else{
playAudio(audio.get(i));
playAudio(audio.get(i));
i++;}
List<String> vocab = databaseAccess.getVocabNameA(setId);
textView2.setText(vocab.get(k));
k++;
textView.setText(+j + "/" + vocab.size());
j++;
databaseAccess.close();
if (j == vocab.size() + 1) {
timer.cancel();
}
}
});
}
}, 0, DURATION);
}
I think I have to add something at the playAudio(audio.get(i)), but I have no idea how should I do. If I do this, it plays the audio at the same time. Thanks.
if (i > audio.size()){
i = 0;
}else{
playAudio(audio.get(i));
playAudio(audio.get(i));
i++; }

Animating the Loading... textview continually

Im trying to animate the three dots of "Loading..." textview as follows,
Handler handler = new Handler();
for (int i = 100; i <= 3500; i =i+100) {
final int finalI = i;
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(finalI %300 == 0){
loadigText.setText("Loading.");
}else if(finalI %200 == 0){
loadigText.setText("Loading..");
}else if(finalI %100 == 0){
loadigText.setText("Loading...");
}
}
}, i);
The problem is that ,
1. Im unable to animate it infinitely till the dialog is visible.
2. Im unable to reduce the speed of Three dots animation,
How can I be able to sort this out
Example:
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int count = 0;
#Override
public void run() {
count++;
if (count == 1)
{
textView.setText("Loading.");
}
else if (count == 2)
{
textView.setText("Loading..");
}
else if (count == 3)
{
textView.setText("Loading...");
}
if (count == 3)
count = 0;
handler.postDelayed(this, 2 * 1000);
}
};
handler.postDelayed(runnable, 1 * 1000);

ImageSlider Using Thread

I want slider in my imageview but it gives me only one image at a time if any one of you have a solution please tell me.
here in my activity class.
images come from the API and there is a whole path of every image and its a web url
ArrayList<String> imagepath1 = i.getExtras().getStringArrayList("event_photo");
for (int i1 = 0; i1 < imagepath1.size(); i1++) {
stockArr = new String[imagepath1.size()];
stockArr = imagepath1.toArray(stockArr);
// imagepath = stockArr.toString();
String imagepath = stockArr[i1];
}
if (!TextUtils.isEmpty(imagepath)) {
if (imagepath.endsWith(".jpeg") || imagepath.endsWith(".jpg")
|| imagepath.endsWith(".gif") || imagepath.endsWith(".png")) {
imageLoader = new ImageLoader(mcontext);
mUpdateResults = new Runnable() {
public void run() {
Animation rotateimage = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.alpha);
for (int i2 = 0; i2 < stockArr.length; i2++) {
imageLoader.DisplayImage(imagepath1.get(i2),
imagetop);
// imagetop.startAnimation(rotateimage);
/*imagetop.setImageResource(number[currentimageindex
% stockArr.length]);*/
}
}
};
final Handler mHandler = new Handler();
int delay = 500; // delay for 1 sec.
int period = 5000; // repeat every 4 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period);
} else {
}
}
Please Help if any one have a solution

android ui is not updating from ui Thread in api 10 after animation

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();

Check all the view's background in an if statement in Android?

I have many buttons(81) from the left and right part of my layout. All in all, I have 162 buttons. I put those buttons to Button[] and I handle it properly.
private int[] right_lung = { R.id.btn_right_106, R.id.btn_right_113,
R.id.btn_right_114, R.id.btn_right_115, R.id.btn_right_116, R.id.btn_right_121,
R.id.btn_right_122, R.id.btn_right_123, R.id.btn_right_124, R.id.btn_right_125,
R.id.btn_right_129, R.id.btn_right_130, R.id.btn_right_131, R.id.btn_right_132,
R.id.btn_right_133, R.id.btn_right_134, R.id.btn_right_137, R.id.btn_right_138,
R.id.btn_right_139, R.id.btn_right_140, R.id.btn_right_141, R.id.btn_right_142,
R.id.btn_right_143, R.id.btn_right_145, R.id.btn_right_146, R.id.btn_right_147,
R.id.btn_right_148, R.id.btn_right_149, R.id.btn_right_150, R.id.btn_right_151,
R.id.btn_right_152, R.id.btn_right_153, R.id.btn_right_154, R.id.btn_right_155,
R.id.btn_right_156, R.id.btn_right_157, R.id.btn_right_158, R.id.btn_right_159,
R.id.btn_right_160, R.id.btn_right_161, R.id.btn_right_162, R.id.btn_right_163,
R.id.btn_right_164, R.id.btn_right_165, R.id.btn_right_166, R.id.btn_right_167,
R.id.btn_right_168, R.id.btn_right_169, R.id.btn_right_170, R.id.btn_right_171,
R.id.btn_right_172, R.id.btn_right_173, R.id.btn_right_174, R.id.btn_right_175,
R.id.btn_right_176, R.id.btn_right_177, R.id.btn_right_178, R.id.btn_right_179,
R.id.btn_right_180, R.id.btn_right_181, R.id.btn_right_182, R.id.btn_right_183,
R.id.btn_right_184, R.id.btn_right_185, R.id.btn_right_186, R.id.btn_right_187,
R.id.btn_right_188, R.id.btn_right_189, R.id.btn_right_190, R.id.btn_right_191,
R.id.btn_right_192, R.id.btn_right_194, R.id.btn_right_195, R.id.btn_right_196,
R.id.btn_right_197, R.id.btn_right_198, R.id.btn_right_199, R.id.btn_right_200,
R.id.btn_right_205, R.id.btn_right_206, R.id.btn_right_207 };
private Button[] btn_right = new Button[right_lung.length];
private int[] left_lung = { R.id.btn_left_7, R.id.btn_left_13, R.id.btn_left_14,
R.id.btn_left_15, R.id.btn_left_16, R.id.btn_left_20, R.id.btn_left_21,
R.id.btn_left_22, R.id.btn_left_23, R.id.btn_left_24, R.id.btn_left_27,
R.id.btn_left_28, R.id.btn_left_29, R.id.btn_left_30, R.id.btn_left_31,
R.id.btn_left_32, R.id.btn_left_34, R.id.btn_left_35, R.id.btn_left_36,
R.id.btn_left_37, R.id.btn_left_38, R.id.btn_left_39, R.id.btn_left_40,
R.id.btn_left_41, R.id.btn_left_42, R.id.btn_left_43, R.id.btn_left_44,
R.id.btn_left_45, R.id.btn_left_46, R.id.btn_left_47, R.id.btn_left_48,
R.id.btn_left_49, R.id.btn_left_50, R.id.btn_left_51, R.id.btn_left_52,
R.id.btn_left_53, R.id.btn_left_54, R.id.btn_left_55, R.id.btn_left_56,
R.id.btn_left_57, R.id.btn_left_58, R.id.btn_left_59, R.id.btn_left_60,
R.id.btn_left_61, R.id.btn_left_62, R.id.btn_left_63, R.id.btn_left_64,
R.id.btn_left_65, R.id.btn_left_66, R.id.btn_left_67, R.id.btn_left_68,
R.id.btn_left_69, R.id.btn_left_70, R.id.btn_left_71, R.id.btn_left_72,
R.id.btn_left_73, R.id.btn_left_74, R.id.btn_left_75, R.id.btn_left_76,
R.id.btn_left_77, R.id.btn_left_78, R.id.btn_left_79, R.id.btn_left_80,
R.id.btn_left_81, R.id.btn_left_82, R.id.btn_left_83, R.id.btn_left_84,
R.id.btn_left_85, R.id.btn_left_86, R.id.btn_left_87, R.id.btn_left_88,
R.id.btn_left_89, R.id.btn_left_90, R.id.btn_left_91, R.id.btn_left_92,
R.id.btn_left_93, R.id.btn_left_94, R.id.btn_left_95, R.id.btn_left_98,
R.id.btn_left_99, R.id.btn_left_100, };
private Button[] btn_left = new Button[left_lung.length];
Whenever I click on the button, just like in mine sweeper game, many buttons are randomly opened. And when it is opened, I am changing its background into R.drawable.affected. The goal of the game is to open all the buttons in left and right. My question is this, how can I check if all of the buttons are set in R.drawable.affected? Because after that, I will execute a method that will congratulate the user. Thanks in advance.
EDIT:
for (int i = 0; i < btn_right.length; i++) {
final int a = i;
int counter_total_affected = 0;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
right_lung.add(a);
btn_right[a].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
} else if (counter == 2) {
for (int i = 0; i < 2; i++) {
int n = i;
right_lung.add(a + n);
}
try {
for (int i = 0; i < 2; i++) {
int n = i;
btn_right[a + n].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (counter == 3) {
right_lung.add(a);
btn_right[a].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
} else if (counter == 4) {
for (int i = 0; i < 25; i++) {
int n = i;
right_lung.add(a + n);
}
try {
for (int i = 0; i < 25; i++) {
int n = i;
btn_right[a + n].setBackgroundResource(R.drawable.affected);
counter_total_affected++;
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (counter == ...) {
//statements...
} else if (counter_total_affected == (btn_left.length + btn_right.length)) {
//CONGRATULATORY METHOD
}
counter++;
}
}
Increment the counter each time you change background of button and compare its value with length of button array. if both are same that means, all button backgrounds are set.
Try this :
for (int i = 0; i < btn_right.length; i++) {
final int b = i;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if btn_right[i].getDrawable().getConstantState().equals
(getResources().getDrawable(R.drawable.affected).getConstantState()))
{
if(counter == btn_right.length){
//Congratulate user...
}
}else{
btn_right[b].setBackgroundResource(R.drawable.affected);
counter++;
}
}
}
U can set Tag:
for (int i = 0; i < btn_right.length; i++) {
final int b = i;
btn_right[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (counter == 1) {
right_affected.add(b);
btn_right[b].setBackgroundResource(R.drawable.affected);
btn_right[b].setTag('1');
} else {
//some stuff here...
}
}
}
after congratulate the user set its Tag to 0.
EDIT:
ArrayList<Integer> arrayofId =new ArrayList<Integer>();
#Override
public void onClick(View v) {
if (counter == 1) {
right_affected.add(b);
btn_right[b].setBackgroundResource(R.drawable.affected);
arrayofId.add(b);//need to convert int to Integer.
} else {
//some stuff here...
}
}
To congratulate:
for(int i=0;i<arrayofId.size();i++)
{
// you can get here id of effected buttons
}

Categories

Resources