A simple countdowntimer that updates the value of a double variable and then display the value onto the end of a textbox. I have done this with integers and it works fine, however when I use a double value it just simply disappears from the end of the textbox. Is there a reason for this occurance?
Code used:
PSICount = new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {
timer2.setText("seconds remaining: " + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
if(alti >= 33000){
Random r = new Random();
double Low = 0.2 ;
double High = 0.5;
double randomValue = Low + (High - Low) * r.nextDouble();
pressure = pressure - randomValue;
pressureT.setText("Pressure: "+ pressure + " PSI");
}
PSICount.start();
}
};
PSICount.start();
This is the timer and it triggers every 5 seconds if the value of another variable is above 33000. As soon as that variable reaches 33000, the textbox (which has a placeholder value of 10.0 at the end) does not display any value at the end. Do I need to do something to use doubles with textViews?
Thank you
My entire Class:
import android.content.pm.ActivityInfo;
import android.graphics.Typeface;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class flight_engineer extends AppCompatActivity {
int fuel = 100;
int alti = 10000;
int speed = 50;
double over_heat = 600;
double pressure = 10.0;
CountDownTimer fuelCount;
CountDownTimer PSICount;
CountDownTimer spike;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flight_engineer);
//sets screen orientation on created
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Typeface typeface = Typeface.createFromAsset(getAssets(), "Stencil WW.ttf");
final TextView fuelT = (TextView) findViewById(R.id.fuelText);
final TextView pressureT = (TextView) findViewById(R.id.presText);
final TextView altitudeT = (TextView) findViewById(R.id.altText);
final TextView speedT = (TextView) findViewById(R.id.speedText);
final TextView over_heatingT = (TextView) findViewById(R.id.heatText);
final TextView info = (TextView) findViewById(R.id.infoText);
final TextView timer = (TextView) findViewById(R.id.timer);
final TextView timer2 = (TextView) findViewById(R.id.timer2);
fuelT.setTypeface (typeface);
pressureT.setTypeface (typeface);
altitudeT.setTypeface (typeface);
speedT.setTypeface (typeface);
over_heatingT.setTypeface (typeface);
info.setTypeface(typeface);
fuelT.setText("Fuel: "+fuel);
pressureT.setText("Pressure: "+ pressure+ " PSI");
altitudeT.setText("Altitude: "+alti+" ft");
speedT.setText("Speed: "+speed+" MPH");
over_heatingT.setText("System Heat: "+over_heat+" °C");
Button speedPlus = (Button) findViewById(R.id.speedPlus);
speedPlus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
fuelCount = new CountDownTimer(2000,1000) {
#Override
public void onTick(long millisUntilFinished) {
timer.setText("seconds remaining: " + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
fuelCount.start();
fuel--;
fuelT.setText("Fuel: "+fuel);
over_heat=over_heat + 4;
fuelT.setText("Fuel: "+fuel);
over_heatingT.setText("System Heat: "+over_heat+" °C");
Random r = new Random();
int Low = 443;
int High = 872;
int Result = r.nextInt(High - Low) + Low;
alti = alti + Result;
altitudeT.setText("Altitude: "+alti+" ft");
}
};
fuelCount.start();
PSICount = new CountDownTimer(5000,1000) {
#Override
public void onTick(long millisUntilFinished) {
timer2.setText("seconds remaining: " + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
if(alti >= 33000){
Random r = new Random();
double Low = 0.2 ;
double High = 0.5;
double randomValue = Low + (High - Low) * r.nextDouble();
pressure = pressure - randomValue;
pressureT.setText("Pressure: "+ pressure + " PSI");
}
PSICount.start();
}
};
PSICount.start();
spike = new CountDownTimer(30000,1000) {
#Override
public void onTick(long millisUntilFinished) {
}
#Override
public void onFinish() {
spike.start();
over_heat = over_heat + 50;
over_heatingT.setText("System Heat: "+over_heat+" °C");
alti = alti + 1000;
altitudeT.setText("Altitude: "+alti+" ft");
if(speed < 300 || speed > 300) {
Random r = new Random();
int Low = -50;
int High = 50;
int Result = r.nextInt(High - Low) + Low;
speed = speed + Result;
speedT.setText("Speed: " + speed + " MPH");
}
}
};
spike.start();
}
}
Related
Hi I am new in android studio and i made a chronometer with a couple of other functions in my app but i have a problem when I change the fragment or screen rotates everything is good except my chronometer is set back to 00:00:00. Can someone help me please and show me how can I keep my chronometer running when those changes are made or even let it run when app is closed...
here is my code:
public class HomeFragment extends Fragment {
private Chronometer chronometer;
private Button buttonStart, buttonMinus;
private TextView mTVCount, gTVCount, lTVCount;
private int mCount;
private Double gCount=0.00, lCount=0.00;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home, container, false);
chronometer = v.findViewById(R.id.simpleChronometer);
gTVCount = v.findViewById(R.id.textView67);
mTVCount = v.findViewById(R.id.textView62);
lTVCount = v.findViewById(R.id.textView69);
buttonStart = (Button) v.findViewById(R.id.button);
buttonMinus = (Button) v.findViewById(R.id.button2);
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
increment();
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.start();
if (mCount > 20) {
Toast.makeText(buttonStart.getContext(), "Du wirst zu einem Schornstein!",
Toast.LENGTH_SHORT).show();
} else if (mCount >= 15) {
Toast.makeText(buttonStart.getContext(), "Du willst doch aufhören...oder?",
Toast.LENGTH_SHORT).show();
} else if (mCount >= 10) {
Toast.makeText(buttonStart.getContext(), "Ok Großer, mach eine Pause!",
Toast.LENGTH_SHORT).show();
} else if (mCount >= 5) {
Toast.makeText(buttonStart.getContext(), "Ganz Langsam!",
Toast.LENGTH_SHORT).show();
} else if (mCount > 0) {
Toast.makeText(buttonStart.getContext(), "Nicht so schlimm...",
Toast.LENGTH_SHORT).show();
}
}
private void increment() {
mCount++;
mTVCount.setText(String.valueOf(mCount));
lCount += 5;
lTVCount.setText(String.valueOf(String.format("%.0f", lCount) + " min"));
if (lCount >= 60) {
lTVCount.setText(String.valueOf(String.format("%.2f", lCount / 60)) + " h");
}
gCount += 0.36;
gTVCount.setText(String.valueOf(String.format("%.2f", gCount) + "€"));
}
});
Chronometer timeElapsed = (Chronometer) v.findViewById(R.id.simpleChronometer);
timeElapsed.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
#Override
public void onChronometerTick(Chronometer cArg) {
long time = SystemClock.elapsedRealtime() - cArg.getBase();
int h = (int) (time / 3600000);
int m = (int) (time - h * 3600000) / 60000;
int s = (int) (time - h * 3600000 - m * 60000) / 1000;
String hh = h < 10 ? "0" + h : h + "";
String mm = m < 10 ? "0" + m : m + "";
String ss = s < 10 ? "0" + s : s + "";
cArg.setText(hh + ":" + mm + ":" + ss);
}
});
buttonMinus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
decrement();
}
private void decrement() {
mCount--;
mTVCount.setText(String.valueOf(mCount));
lCount -= 5;
lTVCount.setText(String.valueOf(String.format("%.0f", lCount) + " min"));
if (lCount >= 60) {
lTVCount.setText(String.valueOf(String.format("%.2f", lCount / 60)) + " h");
}
gCount -= 0.36;
gTVCount.setText(String.valueOf(String.format("%.2f", gCount) + "€"));
}
});
if (savedInstanceState != null){
mCount = savedInstanceState.getInt("count");
mTVCount.setText(String.valueOf(mCount));
gCount = savedInstanceState.getDouble("geld");
gTVCount.setText(String.valueOf( gCount) + "€");
lCount = savedInstanceState.getDouble("leben");
lTVCount.setText(String.valueOf(String.format("%.0f", lCount) + " min"));
if (lCount >= 60) {
lTVCount.setText(String.valueOf(String.format("%.2f", lCount / 60)) + " h");
}
}
return v;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("count", mCount);
outState.putDouble("geld", gCount);
outState.putDouble("leben", lCount);
super.onSaveInstanceState(outState);
}
}
I am trying to make a Pomodoro Timer (25 minutes work, 5 minutes break then repeat n times). But I can't find a way to let the break timer start automatically and then start again with the work timer (work timer = 00:10 - break timer = 00:03, i put these values so i can test faster). How can I fix this?
public class MainActivity extends AppCompatActivity {
TextView tvTimer, tvSessions;
ImageView ivStart, ivStop;
SeekBar seekBar;
CountDownTimer countDownTimer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTimer = findViewById(R.id.tvTimer);
tvSessions = findViewById(R.id.tvSessions);
ivStart = findViewById(R.id.ivStart);
ivStop = findViewById(R.id.ivStop);
seekBar = findViewById(R.id.seekBar);
seekBar.setMax(2500);
seekBar.setProgress(10);
seekBar.setVisibility(View.GONE);
tvTimer.setText("00:10");
}
// ON CLICK METHODS
public void startTimer(View v){
countDownTimer = new CountDownTimer(seekBar.getProgress() * 1000 + 100, 1000) {
#Override
public void onTick(long millisUntilFinished) {
updateTimer((int) millisUntilFinished / 1000);
}
#Override
public void onFinish() {
breakTimer();
}
};
countDownTimer.start();
}
public void stopTimer(View v){
}
// UPDATE METHODS
private void breakTimer(){
seekBar.setProgress(3);
tvTimer.setText("00:03");
}
private void updateTimer(int progress){
int minutes = progress / 60;
int seconds = progress - minutes * 60;
String textMinutes = String.valueOf(minutes);
String textSeconds = String.valueOf(seconds);
if(seconds < 10) textSeconds = "0" + textSeconds;
if(minutes < 10) textMinutes = "0" + textMinutes;
tvTimer.setText(textMinutes + ":" + textSeconds);
}
}
Expected output:
00:10 -> 00:00 -> 00:03 -> 00:00 -> 00:10 and so on...
What about something like this. Using a flag to keep track of if the Timer is a rest timer or a pomodoro timer.
When the timer finishes, you check the flag to decide which timer to start next (and flip the flag so next time it does the opposite).
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.concurrent.TimeUnit;
public class MainActivity extends AppCompatActivity {
private static final int TICK_EVERY_SECOND = 1000;
private TextView tvTimer, tvSessions;
private ImageView ivStart, ivStop;
private SeekBar seekBar;
private CountDownTimer countDownTimer;
private boolean isRest = false;
private int userSelectedDurationSeconds = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvTimer = findViewById(R.id.tvTimer);
tvSessions = findViewById(R.id.tvSessions);
ivStart = findViewById(R.id.ivStart);
ivStop = findViewById(R.id.ivStop);
seekBar = findViewById(R.id.seekBar);
seekBar.setMax(2500);
seekBar.setProgress(10);
seekBar.setVisibility(View.GONE);
tvTimer.setText("00:10");
}
// ON CLICK METHODS
public void startTimer(View v) {
isRest = false;
int durationSeconds = seekBar.getProgress();
userSelectedDurationSeconds = durationSeconds;
restartTimer(durationSeconds);
}
private void restartTimer(int durationSeconds) {
countDownTimer = new CountDownTimer(TimeUnit.SECONDS.toMillis(durationSeconds), TICK_EVERY_SECOND) {
#Override
public void onTick(long millisUntilFinished) {
updateTimer((int) millisUntilFinished / 1000);
}
#Override
public void onFinish() {
breakTimer();
}
};
countDownTimer.start();
}
public void stopTimer(View v) {
}
// UPDATE METHODS
private void breakTimer() {
isRest = !isRest;
if(isRest) {
seekBar.setProgress(3);
tvTimer.setText("00:03");
restartTimer(3);
} else {
restartTimer(userSelectedDurationSeconds);
}
}
private void updateTimer(int progressInSeconds) {
int minutes = progressInSeconds / 60;
int seconds = progressInSeconds - minutes * 60;
String textMinutes = String.valueOf(minutes);
String textSeconds = String.valueOf(seconds);
if (seconds < 10) textSeconds = "0" + textSeconds;
if (minutes < 10) textMinutes = "0" + textMinutes;
tvTimer.setText(textMinutes + ":" + textSeconds);
}
}
I have create a calculate program with some condition. I want to calculate and successive result for four times. Ex. the first result will pass to the first payment, then second result will pass data by getting the old result plus the new result from first payment. Finally calculate all result from them. I don't know how to keep the old result to plus the new result. How can I do it. Below is my code:
public class MainActivity extends Activity {
private double result;
private double used;
private double price1;
private double price2;
private double price3;
private double price4;
private double price5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText use_water = (EditText)findViewById(R.id.et1);
Button btn = (Button)findViewById(R.id.btn);
final TextView pay1 = (TextView)findViewById(R.id.payment1);
final TextView pay2 = (TextView)findViewById(R.id.payment2);
final TextView pay3 = (TextView)findViewById(R.id.payment3);
final TextView pay4 = (TextView)findViewById(R.id.payment4);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
used = Double.parseDouble(use_water.getText().toString());
if(used<=15){
result = used * 550;
pay1.setText(Double.toString(result)+" Reils");
}
else if(used<=25){
result = used * 720;
pay1.setText(Double.toString(result)+" Reils");
}
else if(used<=40){
result = used * 1010;
pay1.setText(Double.toString(result)+" Reils");
}
else {
result = used * 1720;
pay1.setText(Double.toString(result)+" Reils");
}
price2 = result + result;
pay2.setText(Double.toString(result)+" Reils");
price3 = price2+(result + result);
pay3.setText(Double.toString(result)+" Reils");
price4 = price3+(price2+(result + result));
pay4.setText(Double.toString(result)+" Reils");
}
});
}
this example images
Simply, use price2, price3 and price4 as your TextViews content:
price2 = result + result;
pay2.setText(Double.toString(price2)+" Reils");
price3 = price2+(result + result);
pay3.setText(Double.toString(price3)+" Reils");
price4 = price3+(price2+(result + result));
pay4.setText(Double.toString(price4)+" Reils");
Use Reactive programming
For example,
TextView textView1 = (TextView)findViewById(R.id.payment1);;
TextView textView2 = (TextView)findViewById(R.id.payment2);;
TextView textView3 = (TextView)findViewById(R.id.payment3);;
textView1.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) {
//todo let textview2 know
//like textview2.setText(result + Double.parseDouble(s) + "");
}
});
I have a game that spawns 4 enemies on screen from image boxes, when a timer reaches zero, an enemy spawns onto the screen. I want a way to constantly check if the enemies are visible or not on screen.
My class currently containing a while loop to check visibility:
public class rear_gunner extends AppCompatActivity {
int clickcount=0;
CountDownTimer countDownTimer;
int score = 0;
int health2 = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rear_gunner);
final TextView text = (TextView) findViewById(R.id.textView2);
final TextView scoreText = (TextView) findViewById(R.id.score);
final TextView health = (TextView) findViewById(R.id.Health);
health.setText("Health:"+ health2);
//Enemy ImageViews
final ImageView enemy1 = (ImageView) findViewById(R.id.enemy1);
final ImageView enemy2 = (ImageView) findViewById(R.id.enemy2);
final ImageView enemy3 = (ImageView) findViewById(R.id.enemy3);
final ImageView enemy4 = (ImageView) findViewById(R.id.enemy4);
//sets screen orientation on created
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//sets imageViews into array
final ImageView[] enemies = new ImageView[4];
enemies[0] = enemy1;
enemies[1] = enemy2;
enemies[2] = enemy3;
enemies[3] = enemy4;
boolean running = true;
while (!running) {
if (enemy1.getVisibility() == View.VISIBLE) {
int damage = 1;
health2 = health2 - damage;
health.setText("Health:" + health2);
} else {
// Either gone or invisible
}
if (enemy2.getVisibility() == View.VISIBLE) {
int damage = 1;
health2 = health2 - damage;
health.setText("Health:" + health2);
} else {
// Either gone or invisible
}
if (enemy3.getVisibility() == View.VISIBLE) {
int damage = 1;
health2 = health2 - damage;
health.setText("Health:" + health2);
} else {
// Either gone or invisible
}
if (enemy4.getVisibility() == View.VISIBLE) {
int damage = 1;
health2 = health2 - damage;
health.setText("Health:" + health2);
} else {
// Either gone or invisible
}
}
The above code just simply does not work. When I start the activity the screen goes black and remains like that until closed. Does this have anything to do with the way I am using the while loop for running?
My timer code if needed:
//random number generator between 10-25
Random r = new Random();
int Low = 10000;
int High = 25000;
int Result = r.nextInt(High-Low) + Low;
//count down timer for spawing enemies
countDownTimer = new CountDownTimer(Result, 1000) {
#Override
public void onTick(long millisUntilFinished) {
text.setText("seconds remaining: " + millisUntilFinished / 1000);
if (score == 500)
{
countDownTimer.cancel();
}
}
#Override
public void onFinish() {
Random r = new Random();
int Low = 0;
int High = 4;
int Result = r.nextInt(High-Low) + Low;
enemies[Result].setVisibility(View.VISIBLE);
countDownTimer.start();
if (score == 500)
{
countDownTimer.cancel();
}
}
};
countDownTimer.start();
}
}
Thank you for all the help and any advice for a more efficient way of doing this would be greatly appriciated.
boolean running = true;
while (!running) {
...
Your while loop never get chance to run, if you want refresh UI, you must write this code in another place, maybe a background Timer task thread or use Handler to deal with this.
This is a Calculator program where several inputs are used to create several outputs. The single button on the App that I'm writing runs all the calculations then gives the outputs.
I'm getting one consistent "warning" on the lines under public void onClick
The warning is: Variable 'aResult' initializer 'Double.parseDouble(tvaResult.getText().toString())' is redundant
package com.example.ericallenbellville.rcbeamdesign;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnCalc;
private TextView tvaResult;
private TextView tvcResult;
private TextView tvetResult;
private TextView tvphiResult;
private TextView tvMnResult;
private TextView tvphiMnResult;
private TextView tvbeta1Result;
private EditText etB,etD,etAs,etFc,etFy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
btnCalc = (Button)findViewById(R.id.btnCalc);
etB = (EditText)findViewById(R.id.etB);
etD = (EditText)findViewById(R.id.etD);
etAs = (EditText)findViewById(R.id.etAs);
etFc = (EditText)findViewById(R.id.etFc);
etFy = (EditText)findViewById(R.id.etFy);
tvaResult = (TextView)findViewById(R.id.tvaResult);
tvcResult = (TextView)findViewById(R.id.tvcResult);
tvetResult = (TextView)findViewById(R.id.tvetResult);
tvphiResult = (TextView)findViewById(R.id.tvphiResult);
tvMnResult = (TextView)findViewById(R.id.tvMnResult);
tvphiMnResult = (TextView)findViewById(R.id.tvphiMnResult);
tvbeta1Result = (TextView)findViewById(R.id.tvbeta1Result);
btnCalc.setOnClickListener(this);
}
#Override
public void onClick(View view) {
btnCalc = (Button)findViewById(R.id.btnCalc);
Double B = Double.parseDouble(etB.getText().toString());
Double D = Double.parseDouble(etD.getText().toString());
Double As = Double.parseDouble(etAs.getText().toString());
Double Fc = Double.parseDouble(etFc.getText().toString());
Double Fy = Double.parseDouble(etFy.getText().toString());
Double aResult = Double.parseDouble(tvaResult.getText().toString());
Double cResult = Double.parseDouble(tvcResult.getText().toString());
Double etResult = Double.parseDouble(tvetResult.getText().toString());
Double beta1Result = Double.parseDouble(tvbeta1Result.getText().toString());
Double phiResult = Double.parseDouble(tvphiResult.getText().toString());
Double MnResult = Double.parseDouble(tvMnResult.getText().toString());
Double phiMnResult = Double.parseDouble(tvphiMnResult.getText().toString());
switch(view.getId() ) {
case R.id.btnCalc:
if (Fc <= 4000) {
beta1Result = (0.85);
} else if (4000 < Fc && Fc <= 8000) {
beta1Result = ((0.85)-(0.05 * ((Fc - 4000) / (1000))));
} else {
beta1Result = 0.65;
}
aResult = ((Fy * As) / (0.85 * Fc * B));
cResult = (aResult / beta1Result);
etResult = (((D - cResult) / (cResult)) * 0.003);
if (etResult >= 0.005) {
phiResult = (0.9);
} else if (0.002 <= etResult && etResult < 0.005) {
phiResult = (0.65 + (etResult - 0.002) * 0.25 / (0.005 - 0.002));
} else {
phiResult = (0.00);
}
MnResult = (((Fy * As) * (D - (aResult / 2.0))));
phiMnResult = phiResult * MnResult;
tvaResult.setText(String.valueOf(aResult));
tvcResult.setText(String.valueOf(cResult));
tvetResult.setText(String.valueOf(etResult));
tvphiResult.setText(String.valueOf(phiResult));
tvMnResult.setText(String.valueOf(MnResult));
tvphiMnResult.setText(String.valueOf(phiMnResult));
}}
}