How can I use delay on textview for show text? - java

When i click button first time. Program will random text. then when i press button again it will delay slow to show text
my code
public void onClick(View v) {
final MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.yeehaw);
showRandom = !showRandom;
t = new Thread() {
public void run() {
try {
while(showRandom) {
sleep(5);
mp.start();
handler.sendMessage(handler.obtainMessage());
}
} catch(Exception ex) {
ex.printStackTrace();
}
}
};
t.start();
}

Your context isnt clear from your question still i can see that you are trying to delay the thread execution by 5 ms that would not be noticible to you. Increase the sleep duration to see if you overcome you issue.
PFB the detail for the sleep method here
http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html
Thanks!

Related

Change Text after onWindowFocusChange() has been called

I'm trying to make a fighting screen where I have two sprites, and on top of them I have health bars with their health points written (ProgressBar with a TextView on top of it).
I also have a AnimationDrawable. And it is started inside of onWindowsFocusChanged(). I want to have the text in front of the progressBar change after the animation. So, for example, before the animation a bar has 150/150 written and after the animation I want it to change to, for example, 80/150. The thing is, whenever I try to call setText, the app crashes (I guess because onWindowFocusChanged is the last thing that's called). Is there a way to do this?
Here's a snippet of my code (number_one.start() is the starting of the animation):
private void health_bars(int points_one, int points_two){
healthBarOne.setMax(MAX_HEALTH);
healthBarOne.setProgress(points_one);
health_points_one.setText(points_one + "/" + MAX_HEALTH);
healthBarTwo.setMax(MAX_HEALTH);
healthBarTwo.setProgress(points_two);
health_points_two.setText(points_two + "/" + MAX_HEALTH);
}
public void onWindowFocusChanged(final boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if(hasFocus){
Thread th = new Thread(){
public void run(){
number_one.start();
try {
Thread.sleep(2000);
} catch(InterruptedException e){
}
health_bars(new_health_one, new_health_two);
try {
Thread.sleep(2000);
} catch(InterruptedException e){
}
finish();
}
};
th.start();
attackAnimation();
}
}
Thank you for your time!
EDIT:
Error Log
You cannot update UI elements from any other thread than UI. That is basically what the error is saying. To fix this, use runOnUiThread method in Android:
Thread th = new Thread(){
public void run(){
runOnUiThread(new Runnable() {
#Override
public void run() {
health_bars(new_health_one, new_health_two);
}
});
}
}

Android stop loop by click button

i am trying to make a button that when its clicked , it changes its color image and starts a countdowntimer in a method activeDelay() as here:
piscaAutoButton = (Button) rootView.findViewById(R.id.piscaAutoButton);
piscaAutoButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(final View view) {
if (sessionManager.getPisca()) {
sessionManager.setPisca(false);
trigger = false;
piscaAutoButton.setBackgroundResource(R.drawable.button_bg_round);
} else {
sessionManager.setPisca(true);
piscaAutoButton.setBackgroundResource(R.drawable.button_add_round);
trigger = true;
activeDelay(trigger);
}
here is my activeDelay method:
private boolean activeDelay(boolean trigger) {
while (trigger) { // LOOP WHILE BUTTON IS TRUE CLICKED
int timerDelay = manualControl.getDelayPisca(); //input for timer
//delay manual
new CountDownTimer(timerDelay * 1000, 1000) {
public void onFinish() {
System.out.println("sent");
try {
System.out.println("blink button " + manualControl.getBlinkButton());
if (!manualControl.getBlinkButton().isEmpty()) {
MenuActivity.mOut.write(manualControl.getBlinkButton().getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void onTick(long millisUntilFinished) {
}
}.start();
}
return trigger;
}
My problem is that i need the counter keeps going after finished, stopping just when the user clicks again in the button (trigger = false). I am having problems to program that, if someone could help,i know the return inside activeDelay ejects from the method, how can we solve that ,tks
I would suggest you to don't use CountDownTimer(this runs for some specific time period) , instead of this you should use Handler(this run infinitely) . i am sending you handler code.
private Handler handler = new Handler();
//call this when you want to start the timer .
handler.postDelayed(runnable, startTime);
Runnable runnable = new Runnable() {
#Override
public void run() {
// Do here , whatever you want to do(show updated time e.t.c.) .
handler.postDelayed(this, xyz); //xyz is time interval(in your case it is 1000)
}
};
//Stop handler when you want(In your case , when user click the button)
handler.removeCallbacks(runnable);

how to sleep in for loop until finished loading page android

I am making an app to do something in webview automatically.
I want to make a pause between two lines inside (for-loop) until page finished loading without using Thread.sleep because it freezing my application.
this is my code:
webview.loadUrl("http://**********");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
for(int i=1;i<10;i++){
evaluateJavascript( "document.getElementById('select').value=" + i)
evaluateJavascript("document.getElementById('Search').click();")
//wait until finished loading
while( isloading() ){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
evaluateJavascript( "document.getElementById('any_select').value=5")
.
.
.
.
}
public boolean isloading(){
boolean isloading;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webview.evaluateJavascript("(function() { return document.getElementById(\"Loading\").style.display; })();", new ValueCallback<String>() {
#Override
public void onReceiveValue(String s) {
if(s.equals("none")){
isloading=false;
}else{
isloading=true;
}
}
});
}
if(isloading=true)return true;
if(isloading=false)return false;
}
If you don't want to use Thread.sleep then the alternative is to use AsyncTask in your application.
You can do your loading task in doInBackground() method of AsyncTask and call it using new AsyncTaskClass.execute();
You can go through it from here : http://developer.android.com/reference/android/os/AsyncTask.html
You can do something similar (instead of AsyncTask):
Edit:
Timer timer = new Timer();
while( isloading() ){
try {
timer.schedule( new TimerTask(){
public void run() {
//
}
}, delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
If you want to achieve, what i understand, that once your second line is called you want to stop there and when after 1000 ms you want to continue. You can do one thing, copy all the code after your second line and put that in run method of below code:
new Handler().postDelayed(new Runnable()
{
#Override
public void run() {
}
}, 1000);
It will execute you code after 1000ms
If you want to execute sequentially actions happening on UIThread and in backgrounds threads you should be looking for the Bolt Library by Parse/Facebook.
It apply Javascript promises to Android application.
You can go through it from here : https://github.com/BoltsFramework/Bolts-Android

Activity not showing?

I am trying to build an app that works as an alarm clock. I implemented everything with help of the AlarmManager and it works fine. But I have one problem, when the alarm rings it starts an Activity which shows a screen with a button and plays a sound. But it shows only a black screen and vibrates + plays the sound and then after that it shows the alarm screen.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wecker);
laufen = true;
mp = MediaPlayer.create(getApplicationContext(), R.raw.ton);
verstanden =(Button)findViewById(R.id.button1);
verstanden.setOnClickListener(new View.OnClickListener() {public void onClick(View view)
{
finish();
}
});
for (int i=0; i<10;i++)
{
mp.start();
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
What can I do to show the activity and play the sound simultaneously?
Thread.sleep(1000); Blocks your UI Thread hence, the black screen shows up.
Use this :
new Thread( new Runnable() {
public void run() {
try {
// Add loop to play music and vibrate here
} catch (InterruptedException ie) {}
}
) }.start();
You have put Thread.sleep(1000); in your onCreate() method which is on your UI Thread. Your activity's UI only shows up at onResume() which is after onCreate() so it doesn't get there until your sleep commands are finished. You need to create a new Thread and run the vibrator/sleep cycle on that Thread. Usage is shown in Shivam Verma's answer.

Avoid multi-click in image view android

I try to use this code to prevent multi-click in ImageView but it doesn't help.
Boolean isClicked = false;
#Override
public void onClick(View v)
{
if (v == imgClick && !isClicked)
{
//lock the image
isClicked = true;
Log.d(TAG, "button click");
try
{
//I try to do some thing and then release the image view
Thread.sleep(2000);
} catch (InterruptedException e)
{
e.printStackTrace();
}
isClicked = false;
}
}
In the log cat, I can see 5 lines "button click" when I click on ImageView for 5 times as quickly as possible. I can see the log cat print the first line, wait for a while (2 seconds) and then print the next line. I think when I click the ImageView, the fired event is moved to queue in order, isn't it?. So how can I stop that?
I also try to use setEnable() or setClickable() instead of isClicked variable but it doesn't work too.
Just try this working code
Boolean canClick = true; //make global variable
Handler myHandler = new Handler();
#Override
public void onClick(View v)
{
if (canClick)
{
canClick= false; //lock the image
myHandler.postDelayed(mMyRunnable, 2000);
//perform your action here
}
}
/* give some delay..*/
private Runnable mMyRunnable = new Runnable()
{
#Override
public void run()
{
canClick = true;
myHandler.removeMessages(0);
}
};
Instead of sleeping in 2 seconds, I use some task like doSomeThing() method (has accessed UI thread), and I don't know when it completed. So how can I try your way?
//I referred this android link. You can handle thread more efficiently but i hope below code will work for you..
//you try this and
Boolean canClick = true; //make global variable
public void onClick(View v) {
if(canClick){
new DownloadImageTask().execute();
}
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
Log.d("MSG","Clicked");
canClick =false;
//perform your long operation here
return null;
}
protected void onPostExecute(Bitmap result) {
canClick =true;
}
}
You could keep track of the last consumed click upon your View, and based on it either perform the necessary actions, or simply return:
private long calcTime;
private boolean isClickedLately(final long millisToWait)
{
if (System.currentTimeMillis() - calcTime < millisToWait)
return true;
return false;
}
#Override
public void onClick(View v)
{
if (isClickedLately(2000))
return;
calcTime = System.currentTimeMillis();
Log.d(TAG, "consuming button click");
// perform the necessary actions
}
With the millisToWait parameter you can adjust the threshold of "waiting", but if you know that you want to wait exactly 2 seconds between two consecutive clicks, you can eliminate it.
This way you don't have to deal with Threads, which is good, since it's not a great idea to make the gui thread wait.

Categories