I would like to update the progressBar with Handler and for loop but without success.
Code:
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler(Looper.getMainLooper());
for (progress_k = from; progress_k<=to ;progress_k++)
{
handler1.postDelayed(new Runnable()
{
#Override
public void run()
{
FrontLayout.update_splash_progress_bar(progress_k, 100);
}
}, 2000);
}
}
Question:
The progress bar increase immediately to the end value instead of progressively.
Why?
Try this:
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler(Looper.getMainLooper());
for (progress_k = from; progress_k<=to ;progress_k++)
{
final int curr_progress_k = progress_k;
handler1.postDelayed(new Runnable()
{
#Override
public void run()
{
FrontLayout.update_splash_progress_bar(curr_progress_k, 100);
}
}, progress_k * 100); // adjust "100" value to adjust speed
}
}
Repeat a task with a time delay?
#inazaruk
private ProgressBar progressBar;
private Handler mHandler;
private int progressInt = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
progressBar = (ProgressBar) findViewById(R.id.pb);
progressBar.setProgress(0);
mHandler = new Handler();
runnable.run();
}
Runnable runnable = new Runnable() {
#Override
public void run() {
try {
updateProgress();
} catch (Exception ignored) {
} finally {
mHandler.postDelayed(runnable, progressInt);
}
}
};
private void updateProgress() {
progressInt += 1;
if (progressInt > 100) {
mHandler.removeCallbacks(runnable);
} else {
progressBar.setProgress(progressInt);
}
}
try this code:
Solution 1
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler();
class Task implements Runnable {
int start,end;
Task(int a,int b) { start = a; end = b;}
#Override
public void run() {
for (int i =start ; i <= end; i++) {
final int value = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler1.post(new Runnable() {
#Override
public void run() {
progressBar.setProgress(value);
}
});
}
}
}
Thread t = new Thread(new Task(from, to)); //call it
t.start();
}
Solution 2: More Simple
If thread is too much to ask for this problem..
you can use the following solution to use a single Handler to update progressbar:
code
public class HandlerDemo extends Activity
{
ProgressBar bar;
Handler handler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
bar.incrementProgressBy(5);
}
};
boolean isRunning = false;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
bar = (ProgressBar) findViewById(R.id.progress);
}
public void onStart()
{
super.onStart();
bar.setProgress(0);
Thread background = new Thread(new Runnable()
{
public void run()
{
try
{
for (int i = 0; i < 20 && isRunning; i++)
{
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
}
catch (Throwable t)
{
// just end the background thread
}
}
});
isRunning = true;
background.start();
}
public void onStop()
{
super.onStop();
isRunning = false;
}
}
Hope it helps..
Related
Here my transition and reverse transition happens only once not 10 times, Can u tell me where I'm wrong ??
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<10;i++)
{
Resources res = getApplicationContext().getResources();
Drawable background[] = new Drawable[2];
background[0] = res.getDrawable(R.drawable.shapes);
background[1] = res.getDrawable(R.drawable.large_shape);
final TransitionDrawable transition = new TransitionDrawable(background);
ImageView imgview = findViewById(R.id.transitimage);
imgview.setImageDrawable(transition);
Runnable r1 = new Runnable() {
#Override
public void run() {
transition.startTransition(2000);
}
};
Runnable r2 = new Runnable() {
#Override
public void run() {
transition.reverseTransition(2000);
}
};
Handler h = new Handler();
h.postDelayed(r1, 0);
h.postDelayed(r2, 2000);
}
}
}
public void repeat() {
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
if (i>9) { // i is a field,its initial value equals 0
cancel(); // stop timer
}
if (flag) {
transition.startTransition(2000);
} else {
transition.reverseTransition(2000);
}
flag = !flag;
i += 1;
}
public void onFinish() {
repeat();
}
}.start();
}
Call above function in some place.
I am not much experienced in Android Programming, So I am stuck with a problem where I want to animate till the arraySize(). But the loop iterates only once.Actually I need to apps animation where every app start animated after haphazard delay time. For better understanding I am here Posting my Detailed Code.
I have been taking this problem for since 2 days and could not find any solution so i decide to post here.
#Override
public void onAnimationEnd(Animation animation) {
Animation cout = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.optimization);
fan_bg.setAnimation(cout);
animateprogress(mHoloCircularProgressBar);//b8=b7+1;
int remainder = ramOptimizeArrayList.size() / 8;
int b = 0;
for ( int i=0;i<= remainder;i++) {
b = b + 1;
top.setImageDrawable(ramOptimizeArrayList.get(b).getIconlink2());
b = b + 1;
top_left.setImageDrawable(ramOptimizeArrayList.get(b).getIconlink2());
b = b + 1;
top_right.setImageDrawable(ramOptimizeArrayList.get(b).getIconlink2());
b = b + 1;
left.setImageDrawable(ramOptimizeArrayList.get(b).getIconlink2());
b = b + 1;
right.setImageDrawable(ramOptimizeArrayList.get(b).getIconlink2());
b = b + 1;
bottom.setImageDrawable(ramOptimizeArrayList.get(b).getIconlink2());
b = b + 1;
bottom_left.setImageDrawable(ramOptimizeArrayList.get(b).getIconlink2());
b = b + 1;
bottom_right.setImageDrawable(ramOptimizeArrayList.get(b).getIconlink2());
Runnable runnable = new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
top_right.startAnimation(top_left_anim);
}
});
}
};
Handler handler1 = new Handler();
handler1.postDelayed(runnable, 100);
Runnable runnable2 = new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
top.startAnimation(top_anim);
}
});
}
};
Handler handler3 = new Handler();
handler3.postDelayed(runnable2, 50);
Runnable runnable4 = new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
top_left.startAnimation(top_right_anim);
}
});
}
};
Handler handler4 = new Handler();
handler4.postDelayed(runnable4, 200);
Runnable runnable5 = new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
left.startAnimation(left_anim);
}
});
}
};
Handler handler5 = new Handler();
handler5.postDelayed(runnable5, 500);
Runnable runnable6 = new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
right.startAnimation(right_anim);
}
});
}
};
Handler handler6 = new Handler();
handler6.postDelayed(runnable6, 300);
Runnable runnable7 = new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
bottom_left.startAnimation(bottom_left_anim);
}
});
}
};
Handler handler7 = new Handler();
handler7.postDelayed(runnable7, 600);
Runnable runnable8 = new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
bottom.startAnimation(bottom_anim);
}
});
}
};
Handler handler8 = new Handler();
handler8.postDelayed(runnable8, 350);
Runnable runnable9 = new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
bottom_right.startAnimation(bottom_right_anim);
}
});
}
};
Handler handler9 = new Handler();
handler9.postDelayed(runnable9, 650);
}
/*top.startAnimation(top_anim);
top_left.startAnimation(top_right_anim);
left.startAnimation(left_anim);
right.startAnimation(right_anim);
bottom_left.startAnimation(bottom_left_anim);
bottom.startAnimation(bottom_anim);
bottom_right.startAnimation(bottom_right_anim);*/
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
If you wanna iterate till array size, why are you using remainder in the loop. You have set the remainder value to:
int remainder = ramOptimizeArrayList.size() / 8;
Since remainder is int, if the array size is say 13:
remainder = 13/8, which turns out to be 1, since remainder is an integer.
Write your for loop as:
for ( int i=0;i<= ramOptimizeArrayList.size();i++) {
//rest of your code
}
So I have thread where it checks every 10ms's if drag is almost outside draggingzone. Basicly my thread code is doing nothing 99% of time so should I make it to pause and resume only when needed? Or does this literally do nothing when right and left are false?
My code looks like this
timer = new Thread() { //new thread
public void run() {
b = true;
try {
do {
sleep(10);
runOnUiThread(new Runnable() {
#Override
public void run() {
if (right) {
dragzone.moveleft(-5);
} else if (left) {
dragzone.moveleft(5);
}
}
});
}
while (b);
} catch (InterruptedException e) {
}
}
;
};
timer.start();
It looks like using a Thread here is not necessary, and you should switch to using a Handler and postDelayed()
First, declare your Handler, boolean, and a Runnable as instance variables:
Handler handler;
boolean b;
Runnable checkDragZone = new Runnable(){
#Override
public void run() {
if (right) {
dragzone.moveleft(-5);
} else if (left) {
dragzone.moveleft(5);
}
if (b){
handler.postDelayed(this, 10);
}
}
};
To start monitoring, set b to true, and start the Runnable:
handler = new Handler();
b = true;
handler.postDelayed(checkDragZone, 10);
To stop it (temporarily or permanently), just set b to false:
b = false;
It's not really a good practice to keep it running. You can start it when you detect the Drag action and then release it when it's finished.
Runnable runnable;
Thread globalThread;
public void startThread() {
if (threadController) {
runnable = new Runnable() {
#Override
public void run() {
while (threadController) {
for (int i = 0; i < adapter.getCount(); i++) {
final int value = i;
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
#Override
public void run() {
viewPager.setCurrentItem(value, true);
}
});
}
}
}
};
globalThread = new Thread(runnable);
globalThread.start();
} else {
return;
}
}
#Override
public void onPause() {
super.onPause();
threadController = false;
handler.removeCallbacks(runnable);
runnable = null;
if (globalThread != null) {
globalThread.interrupt();
}
}
#Override
public void onDestroy() {
super.onDestroy();
threadController = false;
}
Your resolve must be like this globalThread.interrupt();
I want to show numbers from 1 to 100 in sequel order in the TextView and to wait 1 second after printing each number. I also want to implement it using Android services.
I don't know the difference between UIHandler and Handler. When I google about this issue, all I am getting is the difference between handler and a thread.
Please help me out of this,
Thanks in advance
private static final int SHOW_MESSAGE = 1;
private static final int m_cdelay = 1000;
private UIHandler m_cUIHandler;
public int m_cI= 0;
TextView m_cTextShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_cTextShow = (TextView) findViewById(R.id.textview1);
for(m_cI=1; m_cI <= 100; m_cI++){
//m_cUIHandler = new UIHandler();
//m_cUIHandler.sendEmptyMessageDelayed(SHOW_MESSAGE, 1000);
showMessage(m_cI);
}
}
private void showMessage(int m_cI2) {
for(m_cI=1; m_cI <= 100; m_cI++){
m_cTextShow.setText(""+m_cI);
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.sleep(m_cdelay);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}).start();
}
}
#Override
protected void onResume() {
super.onResume();
startService(new Intent(this, NumberService.class));
}
public final class UIHandler extends Handler {
#Override
public void handleMessage(Message pObjMessage) {
switch(pObjMessage.what) {
case SHOW_MESSAGE:
m_cTextShow.setText(""+m_cI);
break;
}
}
}
You can actually rewrite your code like that to make it probably work.
Pls test it and respond.
public void showMessage(int number){
runOnUiThread(new Runnable(){
public void run() {
//Write your number onto the screen
}
});
}
protected void onCreate(Bundle savedInstanceState) {
//Blablabla...
for(m_cI=1; m_cI <= 100; m_cI++){
//m_cUIHandler = new UIHandler();
//m_cUIHandler.sendEmptyMessageDelayed(SHOW_MESSAGE, 1000);
showMessage(m_cI);
Thread.sleep(1000);
}
}
I have a problem that I want to paste images on ImageView in Android and that images are periodically changed after some interval. Means one by one images shown in ImageView. I am doing this with the help of Thread in java but I got some problem that Thread is not attached and something. Please review my code given below and tell me the exact error and how to remove that error or give me some diffrent way for doing this.
package com.ex.thread;
import com.ex.thread.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
public class thread extends Activity implements Runnable{
/** Called when the activity is first created. */
public static Integer[] mThumbIds = {
R.drawable.al1,R.drawable.al2,R.drawable.al3,R.drawable.al4,
};
Thread th;
ImageView iv;
public void run()
{
for(int i=0;i<3;i++)
{
iv.setImageResource(mThumbIds[i]);
System.out.println("Sanat Pandey");
try{
Thread.sleep(3000);
}catch(Exception e)
{
System.out.println(e);
}
}
}
public void create()
{
Thread th = new Thread(new thread());
th.start();
try{
Thread.sleep(3000);
}catch(Exception e)
{
System.out.println(e);
}
}
#Override
public void onCreate(Bundle savedInstace)
{
super.onCreate(savedInstace);
setContentView(R.layout.main);
create();
}
}
Try this..It works out well...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);``
//
//
int []imageArray={R.drawable.img1,R.drawable.img2,R.drawable.img3};
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i=0;
public void run() {
imageView.setImageResource(imageArray[i]);
i++;
if(i>imageArray.length-1)
{
i=0;
}
handler.postDelayed(this, 50); //for interval...
}
};
handler.postDelayed(runnable, 2000); //for initial delay..
}
You can't use things in the UI thread from a background one. So this call:
iv.setImageResource(mThumbIds[i]);
Has to be done in the main thread. In fact you probably don't need a background thread at all to get the effect you're looking for. You can make that just an activity, no need to implement runnable. and then do something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv = (ImageView) findViewById(R.id.yourImageViewID);
int i = 0;
Runnable r = Runnable(){
public void run(){
iv.setImageResource(mThumbIds[i]);
i++;
if(i >= mThumbIds.length){
i = 0;
}
iv.postDelayed(r, 3000); //set to go off again in 3 seconds.
}
};
iv.postDelayed(r,3000); // set first time for 3 seconds
Try this
it's working
public class vv extends Activity {
int b[] = {R.drawable.a, R.drawable.m, R.drawable.b, R.drawable.j, R.drawable.er, R.drawable.chan, R.drawable.vv};
public ImageView i;
int z = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i = (ImageView) findViewById(R.id.image);
i.setImageResource(b[0]);
Thread timer = new Thread() {
public void run() {
try {
sleep(2000);
for (z = 0; z < b.length + 2; z++) {
if (z < b.length) {
sleep(2000);
runOnUiThread(new Runnable() {
public void run() {
i.setImageResource(b[z]);
}
});
} else {
z = 0;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("finally");
}
}
};
timer.start();
}
}
try this code.the images was saved in drawable. please do insert a imageview in xml code. noted that the time interval for the following code is 1 sec.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
public ImageView iv;
public static Integer[] mThumbIds = {
R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4};
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView);
i=0;
t.start();
}
Thread t = new Thread() {
#Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
iv.setImageResource(mThumbIds[i]);
i++;
if(i >= mThumbIds.length){
i = 0;
}}});}}
catch (InterruptedException e) {
}}};
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final int img[] = {R.drawable.flower1, R.drawable.flower2, R.drawable.flower3, R.drawable.flower4};
layout = (RelativeLayout) findViewById(R.id.activity_main);
final Handler handler=new Handler();
Runnable runnable = new Runnable() {
int i = 0;
#Override
public void run() {
layout.setBackgroundResource(img[i]);
i++;
if (i > img.length - 1) {
i = 0;
}
handler.postDelayed(this, 4000); //for interval 4s..
}
};handler.postDelayed(runnable, 100); //for initial delay..
}