Simple while-loop doesnt work - java

I got a little problem. I think the solution is very simple but unfortunately I can't find it. I hope someone can help me
I got a while-loop who has to count up to ten and write the number into a TextView.
It still doesn't work ...
Thanks for your help!
Here is the code:
package de.androidnewcomer.animation;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import static android.R.attr.button;
import static de.androidnewcomer.animation.R.id.textView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView ball=(ImageView)findViewById(R.id.ball);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
count();
break;
}
}
private void count() {
TextView textView=(TextView)findViewById(R.id.textView);
int i;
i=1;
while(i<10) {
i++;
textView.setText(i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Using setText() with an integer value is for setting a string resource reference. To set the text itself, you have to provide a string: Use setText("" + i); and it should work.

The textView.setText(..) needs a string object, but you use a int. You have to convert your int into a string with the following possible options:
You can use String.valueOf(i): textView.setText(String.valueOf(i));
You can use Integer.toString(i): textView.setText(Integer.toString(i));
You can use the empty string literal: textView.setText("" + i);
I prefer the last option. With your code, it should looks like the following code:
private void count() {
TextView textView=(TextView)findViewById(R.id.textView);
int i;
i=1;
while(i<10) {
i++;
textView.setText("" + i);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
You can use a for loop instead of a while loop, like the following code:
private void count() {
TextView textView=(TextView)findViewById(R.id.textView);
for(int i = 1; i < 11; i++){ // From 1 to 10
textView.setText("" + i);
Thread.sleep(200);
}
}

Use a CountDown because you are blocking the main thread
CountDownTimer countDownTimer = new CountDownTimer(2000 /*amount*/, 200/*step*/) {
public void onTick(long millisUntilFinished) {
textView.setText("what ever you want");
}
public void onFinish() {
textView.setText("Done");
}
};
countDownTimer.start();

Related

Running animation after animation, android studio

I am new at android programming and I have a problem to run animation after animation in android studio. The thing is that I don't have specified number of animations I need, it depends on counter.
package com.example.smartpc.memorygame;
import android.graphics.drawable.TransitionDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.Random;
public class MemoryGame extends AppCompatActivity {
ImageView imgRed;
ImageView imgGreen;
ImageView imgBlue;
Button btnStart;
ArrayList<Integer>array=new ArrayList<>();
int counter=3;
int i=0;
Boolean flag=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_memory_game);
imgRed=(ImageView)findViewById(R.id.imgRed);
imgGreen=(ImageView)findViewById(R.id.imgGreen);
imgBlue=(ImageView)findViewById(R.id.imgBlue);
btnStart=(Button)findViewById(R.id.btnStart);
final RotateAnimation animation=new RotateAnimation(0f,360f, RotateAnimation.RELATIVE_TO_SELF,0.5f,
RotateAnimation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(1000);
// animation.setRepeatCount(1);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
;
}
#Override
public void onAnimationEnd(Animation animation) {
if (flag==true) {
i++;
Random rand = new Random();
int number = Math.abs(rand.nextInt() % 3);
btnStart.setText(Integer.toString(i) + " " + Integer.toString(number));
if (number == 0) {
imgRed.startAnimation(animation);
} else if (number == 1) {
imgGreen.startAnimation(animation);
} else if (number==2){
imgBlue.startAnimation(animation);
}
if (i == counter - 1) {
i = 0;
flag=false;
return;
}
}
}
#Override
public void onAnimationRepeat(Animation animation) {
;
}
});
imgRed.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imgRed.startAnimation(animation);
flag=false;
}
});
imgGreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imgGreen.startAnimation(animation);
flag=false;
}
});
imgBlue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imgBlue.startAnimation(animation);
flag=false;
}
});
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// imgGreen.setImageResource(R.drawable.transition);
// ((TransitionDrawable) imgGreen.getDrawable()).startTransition(3000);
flag=true;
Random rand = new Random();
int number = Math.abs(rand.nextInt() % 3);
if (number == 0) {
imgRed.startAnimation(animation);
}
else if (number == 1) {
imgGreen.startAnimation(animation);
}
else if (number==2) {
imgBlue.startAnimation(animation);
}
// btnStart.setText(Integer.toString(i) + " " + Integer.toString(number));
}
});
}
}
In method onAnimationEnd() is problem. It generates number and starts good animaton, but it also starts another one (random, there's no rule which one). Does anyone have idea how to solve this? Also, counter is not fixed, I want to change it in code later.
I will try my best to explain it.
When the process run into onAnimationEnd(), it does mean the animation is finished. It just like to wait for the onAnimationEnd(). And then you call startAnimation(sameAnimation), we can see the method source code.
/**
* Start the specified animation now.
*
* #param animation the animation to start now
*/
public void startAnimation(Animation animation) {
animation.setStartTime(Animation.START_ON_FIRST_FRAME);
setAnimation(animation);
invalidateParentCaches();
invalidate(true);
}
Notice this code : animation.setStartTime(Animation.START_ON_FIRST_FRAME);
If you set the start time with -1, the animation we have run previously will be reset. Oops, because we said the animation is not really finished, it will rerun as the last time.
Run your progress, if you see there are two images rotating at the same time, one of them must be the last one which has rotated.
If you want to run animation after animation, you may not use the same animation in onAnimationEnd(). Or you can use View.clearAnimation() to clear all animation which attach to the view.
I'm glad if it could help you. And if there is anything wrong, please add a comment.

TextWatcher implementation with RadioButtons

Please see attached image and code snippet to aid in explanation.
From the attached image I would like the user to enter a cost, quantity and select either Include Tax or Exclude tax and a new cost is automatically generated where indicated without pressing a Button, but to no avail I am unable to do this. Someone please help. Thanks
See Image Here
After implementing the Changes that were suggested and trying to enter an input in the cost field I was met with the error seen below. Please provide additional feedback. Thanks
Error image
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
public class Calculator extends Fragment {
private static EditText itemText, editCost, editQuantity, calCost, rTax;
private static RadioGroup rGroup;
private static RadioButton rb;
View gView;
private double bTotal = 0, aTotal = 0, trueCost = 0, taxValue = 16.5, cost = 0, newCost = 0;
private int quantity = 1;
DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(Locale.US);
DecimalFormat decimalFormat = new DecimalFormat("###,###.##", symbols);
CalculatorListener activityCommander;
public interface CalculatorListener {
void addtoCart(String itemName, int qty, double beforeTax, double afterTax, double bTotal, double aTotal);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
activityCommander = (CalculatorListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString());
}
}
public Calculator() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
gView = inflater.inflate(R.layout.fragment_calculator, container, false);
editCost = (EditText) gView.findViewById(R.id.editcost);
itemText = (EditText) gView.findViewById(R.id.itemText);
editQuantity = (EditText) gView.findViewById(R.id.editquantity);
calCost = (EditText) gView.findViewById(R.id.calcost);
rTax = (EditText) gView.findViewById(R.id.rtax);
rGroup = (RadioGroup) gView.findViewById(R.id.rgroup);
final ImageButton FieldButton = (ImageButton) gView.findViewById(R.id.FieldButton);
final ImageButton TaxButton = (ImageButton) gView.findViewById(R.id.TaxButton);
final ImageButton CalButton = (ImageButton) gView.findViewById(R.id.CalButton);
rTax.setEnabled(false);
calCost.setEnabled(false);
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
rb = (RadioButton)gView.findViewById(checkedId);
}
});
editCost.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
try{
update();
}catch (NumberFormatException e)
{
e.printStackTrace();
}
}
});
editQuantity.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
try{
update();
}catch (NumberFormatException e)
{
e.printStackTrace();
}
}
});
FieldButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
clearfield();
}
}
);
TaxButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
adjtax();
}
}
);
CalButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
//toCart();
}
}
);
return gView;
}
public void clearfield() {
editCost.setText("");
editCost.setBackgroundResource(R.drawable.edittxt);
editQuantity.setText("");
editQuantity.setBackgroundResource(R.drawable.edittxt);
calCost.setText("");
calCost.setBackgroundResource(R.drawable.edittxt);
itemText.setText("");
itemText.setBackgroundResource(R.drawable.edittxt);
rGroup.clearCheck();
}
public void adjtax() {
editCost.setBackgroundResource(R.drawable.edittxt);
editQuantity.setBackgroundResource(R.drawable.edittxt);
calCost.setBackgroundResource(R.drawable.edittxt);
itemText.setBackgroundResource(R.drawable.edittxt);
rTax.setEnabled(true);
rTax.setText("");
rTax.setBackgroundResource(R.drawable.edittxtfocus);
rTax.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
rTax.setEnabled(true);
} else {
rTax.setEnabled(false);
v.setBackgroundResource(R.drawable.edittxt);
}
}
});
}
public void update(){
if (rTax.getText().toString().isEmpty()) {
taxValue = 16.5;
} else if (!rTax.getText().toString().isEmpty()) {
taxValue = Double.parseDouble(rTax.getText().toString());
}
//CHECKS THE TAX VALUE IF IT NEEDS TO BE CONVERTED
if (taxValue > 1) {
taxValue = taxValue / 100;
} else {
taxValue = taxValue * 1;
}
//CUSTOM VALIDATOR FOR QUANTITY FIELD
if (editQuantity.getText().toString().isEmpty()) {
quantity = 1;
} else if (!editQuantity.getText().toString().isEmpty()) {
quantity = Integer.parseInt(editQuantity.getText().toString());
}
if(rb.getText() == "Include Tax"){
newCost = (((cost = Double.parseDouble(editCost.getText().toString())) * taxValue) + cost) * quantity;
calCost.setText(decimalFormat.format(newCost).toString());
}
else if(rb.getText() == "Exclude Tax"){
newCost = ((cost = Double.parseDouble(editCost.getText().toString())) * quantity);
calCost.setText(decimalFormat.format(newCost).toString());
}
trueCost = cost * quantity;
bTotal = trueCost;
aTotal = newCost;
}
}
Move the rgroup.setOnCheckedChangeListener out of the update() method and into the onCreateView(). You should not have to set the listener every time the text has been updated.
The update method called after text entry can probably just update the tax value if a valid value has been entered and either the check boxes have been selected.
Update with another suggestion
I would lookup the radio button by comparing text as you are doing, some time in the future you may want to change the text in the resource file or apply another locale and this code will stop working.
if(rb.getText() == "Include Tax")
I would suggest comparing against the id itself:
if (checkedId == R.id.radio1 )
Another Suggestion:
Consider changing your variable names to lead with a lower case character. Leading with an upper case letter makes it look like either class name or a constant and make the code a bit more difficult to read.
private EditText itemText;
private EditText editCost;
private EditText editQuantity;
private EditText calcost;
private EdtiText rTax;
You can also remove all(some) the focus change listeners you have and set those attributes in the android drawable resources. Take a look here.
Update 9/9/2016 22:22
A bit better, but as you've found out calls to update can throw a null pointer if rb had never been initialized. You should check for a null rb in the update method and give the user a notice to select an option. You could also assign rb to either of the two values from the start in the onCreateView, so it is never null.
You should probably also add a call to update() after setting rb in the radio group callback. This will allow the screen to update as soon as they choose an option.

Checkboxes and if-else statements

This is the source for a small app I'm making in Android Studio. When I call this function its suppose to compare dog,cat, and parrot to each other and then increment int dogCounter by 5. When I run the function however, It does not update the score.
dogCounter= 0;
catCounter = 0;
//check boxes
cutestCheckBoxDog = (CheckBox)findViewById(R.id.CheckboxCutestDog);
cutestCheckBoxCat = (CheckBox)findViewById(R.id.CheckboxCutestCat);
cutestCheckBoxParrot =(CheckBox)findViewById(R.id.CheckboxCutestParrot);
//call methods
processCutest(cutestCheckBoxDog, cutestCheckBoxCat, cutestCheckBoxParrot);
showResultButton= (Button)findViewById(R.id.showResults);
showResultButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),catCounter + " " + dogCounter, Toast.LENGTH_LONG).show();
public void processCutest(CheckBox dog, CheckBox cat, CheckBox parrot){
if (dog.isChecked() && !cat.isChecked() && !parrot.isChecked()){
dogCounter += 5;
}else if (cat.isChecked() && !dog.isChecked() && !parrot.isChecked()){
catCounter += 5;
} else{
//nobody gets points
}
}
edit: Sorry for poor organization. Still pretty new, pointers on that would be nice as well.
package dogorcatperson.ivellapplication.com.dogorcatperson;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private RadioGroup canineRadioGroup;
private RadioButton canineRadioButton;
private SeekBar seekBar;
private TextView seekBarTextView;
private CheckBox cutestCheckBoxDog;
private CheckBox cutestCheckBoxCat;
private CheckBox cutestCheckBoxParrot;
private RadioGroup droolRadioGroup;
private RadioButton droolRadioButton;
private Button showResultButton;
private int dogCounter;
private int catCounter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//call setup()
setUp();
//seekbar
seekBar = (SeekBar) findViewById(R.id.seekBarFeline);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekBarTextView.setText("comfortableness: " + progress + "/" + seekBar.getMax());
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void setUp(){
dogCounter= 0;
catCounter = 0;
canineRadioGroup =(RadioGroup)findViewById(R.id.radioGroupCanine);
droolRadioGroup = (RadioGroup)findViewById(R.id.RadioGroupDrool);
seekBarTextView = (TextView)findViewById(R.id.seekBarProgressTextView);
//check boxes
cutestCheckBoxDog = (CheckBox)findViewById(R.id.CheckboxCutestDog);
cutestCheckBoxCat = (CheckBox)findViewById(R.id.CheckboxCutestCat);
cutestCheckBoxParrot = (CheckBox)findViewById(R.id.CheckboxCutestParrot);
//call methods
processCutest(cutestCheckBoxDog, cutestCheckBoxCat, cutestCheckBoxParrot);
processDrool(droolRadioGroup);
processCanine(canineRadioGroup);
showResultButton= (Button)findViewById(R.id.showResults);
showResultButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),catCounter + " " + dogCounter, Toast.LENGTH_LONG).show();
// Intent i = new Intent(MainActivity.this, ResultActivity.class);
// i.putExtra("catCounter", catCounter);
// i.putExtra("dogCounter", dogCounter);
// startActivity(i);
}
});
}
public void processCutest(CheckBox dog, CheckBox cat, CheckBox parrot){
if (dog.isChecked() && !cat.isChecked() && !parrot.isChecked()){
dogCounter += 5;
}else if (cat.isChecked() && !dog.isChecked() && !parrot.isChecked()){
catCounter += 5;
} else{
//nobody gets points
}
}
public void processDrool(final RadioGroup radioGroup){
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioId= radioGroup.getCheckedRadioButtonId();
droolRadioButton = (RadioButton)findViewById(radioId);
if (droolRadioButton.getText().equals("yes")){
dogCounter+= 5;
}else if (droolRadioButton.getText().equals("no")){
catCounter+= 5;
}
}
});
}
public void processCanine(final RadioGroup radioGroup){
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioId= canineRadioGroup.getCheckedRadioButtonId();
canineRadioButton = (RadioButton)findViewById(radioId);
if (canineRadioButton.getText().equals("yes")){
catCounter+= 5;
}else if (canineRadioButton.getText().equals("no")){
dogCounter+= 5;
}
}
});
}
}
I think you just need to change your processCutest call so that it goes inside the onClick method, but before the toast.makeText call. Like this:
showResultButton= (Button)findViewById(R.id.showResults);
showResultButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
processCutest(cutestCheckBoxDog, cutestCheckBoxCat, cutestCheckBoxParrot);
Toast.makeText(getApplicationContext(),catCounter + " " + dogCounter, Toast.LENGTH_LONG).show();
I'm assuming that the actual checkboxes are defined elsewhere. Are your checkboxes showing up? If so you might want to put the processDrool and processCanine calls right there as well.

Looking To Set An int Answer To The Form Of A Currency

I am coding my first app in java, it is a simple Tip Calculator. The problem I am having is I am having a hard time trying to figure out how I can get my final answer to come back properly formatted in US currency form ($##.##) Whenever I would try something like setting up a currency string in my code it would say it is being ignored by my strings and this was not doing anything. Here is my main Java code:
package com.rockdrummer.calculating.calculating;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.util.Locale;
public class MainScreen extends Activity {
Button ten, fifteen, twenty;
TextView tip;
EditText bill_amount;
double a;
double b;
double af;
double bf;
double ac;
double bc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
initControls();
Locale.setDefault(Locale.US);
}
protected void initControls() {
ten = (Button) findViewById(R.id.bTen);
fifteen = (Button) findViewById(R.id.bFifteen);
twenty = (Button) findViewById(R.id.bTwenty);
tip = (TextView) findViewById(R.id.tip);
bill_amount = (EditText) findViewById(R.id.bill);
tip.setFilters(new InputFilter[]{
new InputFilter.LengthFilter(20)});
ten.setOnClickListener(new Button.OnClickListener() {
public void onClick(View ten) {
try {
calculate();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
});
fifteen.setOnClickListener(new Button.OnClickListener() {
public void onClick(View fifteen) {
try {
calculate_f();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
});
twenty.setOnClickListener(new Button.OnClickListener() {
public void onClick(View twenty) {
try {
calculate_t();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
});
}
private void calculate() {
a = Double.parseDouble(bill_amount.getText().toString());
b = a * 0.1;
tip.setText("You Should Tip $" + Double.toString(b));
}
private void calculate_f() {
af = Double.parseDouble(bill_amount.getText().toString());
bf = af * 0.15;
tip.setText("You Should Tip $" + Double.toString(bf));
}
private void calculate_t() {
ac = Double.parseDouble(bill_amount.getText().toString());
bc = ac * 0.20;
tip.setText("You Should Tip $" + Double.toString(bc));
}
}
I appreciate any help. Thank you in advance!
With doubles, you are going to need to apply a rounding mode, to avoid any inconsistent bits. You will get some strange results if you don't. The easiest method I've found is using the Java class BigInteger:
http://developer.android.com/reference/java/math/BigInteger.html
Then use DecimalFormat:
http://developer.android.com/reference/java/text/DecimalFormat.html

android development loop problems with timer

i need some help looping a timer in android. I need it to loop 5 times and then stop at 0 on the last run/countdown, but can seems to get it to work. I know it might be a simple.
Any help would be appreciated, thanks to anyone that does help.
package com.project.secondproject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private int countdownValue;
private TextView textfield;
private Handler handler;
private boolean Running;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start(View view){
int countdownValue = 30;
int counter = 0;
Running = true;
textfield = (TextView)findViewById(R.id.Timer);
handler = new Handler();
Runnable runnable = new Runnable(){
#Override
public void run(){
while(Running){
try{
//This controls the interval of the timer. every 1 second
Thread.sleep(1000);}
catch(InterruptedException e){
e.printStackTrace();
}
handler.post(new Runnable(){
#Override
public void run(){
countdownValue -= 1;
textfield.setText(String.valueOf(":" + countdownValue));
if(countdownValue == 1){
Running = false;
}
}
});
}
}
};
new Thread(runnable).start();
}
public void pause(View view){
Running = false;
}
}//End of class
Hope this is what you are looking for. Try this.!
public void run()
{
while(Running){
for (int counter = 5; counter > 0; counter++)
{
System.out.println(" loop..." + counter);
try{Thread.sleep(1000);} // 1 second pause
catch(Exception e){}
}
}

Categories

Resources