I want to display the income of a person in a certain time. For this I ask for the income per month (=gehalt) and working hours per week (stunden) in another activity. Then in the second activity I want to increase the TextView showGehaltproSekunde (id = textViewZahl) every second by the income per second.
I am a beginner, so I don't know what exactly I have to write in public void run(). Or is there another possibility to increase the number every second?
I hope someone can help me. Thank you!
public class SecondScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondlayout);
Timer t, timer;
Intent getGehalt = getIntent();
float gehalt = getGehalt.getFloatExtra("Gehalt", 0);
Intent getStunden = getIntent();
float stunden = getStunden.getFloatExtra("Stunden", 0);
double gehaltProSekunde = gehalt/4/stunden/3600;
double gehaltProSekundeRounded = Math.round(gehaltProSekunde*1000)/1000.0;
TextView showGehaltProSekunde = (TextView)findViewById(R.id.textViewZahl);
showGehaltProSekunde.setText(gehaltProSekundeRounded+" €");
t = new Timer();
t.scheduleAtFixedRate( new TimerTask() {
#Override
public void run() {
}
});
}
}
For example analysis this:
private Handler mHandler = new Handler();
private boolean wasRun = true;
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
if(wasRun){
//whatever you want to do if run
//you can add you want to increase variable here
}
mHandler.postDelayed(this, 1000);
}
}, 1000); // 1 seconds
Just to give some advice not strictly related to your question (#altan yuksel answered it well), i just wanted to throw my opinion in on your calculations.
You calculate the monthly salary to seconds using salary/4/hours worked in a week/3600(seconds in an hour) however not all months have exactly 4 working weeks (actually only February).
May i suggest asking for yearly salary and performing:yearly salary / 52 / hours worked in a week / 3600
Or with your current input data:(monthly salary * 12) / 52 / hours worked in a week / 3600
If you try these to your own you may find them to be a little more accurate.
Related
I am thinking about making an android app to support a game of mine, and I can't find info on how to do it. Here is an app explanation:
The app should have two timers (both set for x minutes). When first player starts his turn (by a button click) his time starts running out. when first player makes his move, he should click his time to stop it, and player two starts his turn.
NOW, the problem I can't find a solution for - When player one takes his turn, time that he loses should pass to the other player's timer.
Example: Both players start with a 5 minute timer. If player one takes 30 second for his turn, his time should go down to 04:30, at which time he clicked on his timer. During that time, for each second he lost, the other player gained time on his timer, so at the beginning of his turn, his time is 05:30. The time goes back and forth, and the player whose timer runs out loses the game.
Any idea how to do this?
I am still stuck to the idea on how to make it, so I have no code to share.
Thank you all for your answers, and effort in helping me, if you have a question I haven't covered, I will gladly answer it.
First, you will need to instantiate class of CountDownTimer everytime you click on button.
For reference check here: https://developer.android.com/reference/android/os/CountDownTimer.html
You have two parameters long millisInFuture, long countDownInterval
Before you start, you will need to create two global variables one for first player, and one for second player like this:
long firstPlayerRemainingTime = 5 * 60 * 1000; // start time 5 minutes
long secondPlayerRemainingTime = 5 * 60 * 1000;
long limitedTime = 30 * 1000; // 30 seconds
CountDownTimer mCountDownTimer;
Now we have came to the most important part and that is logic inside onClickListener method
I don't know if there are two buttons or one, but I will go with two buttons:
btnFirstPlayer.setTag(1); // start timer
btnFirstPlayer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (btnFirstPlayer.getTag() == 1) {
startTimer();
} else {
stopTimer();
}
});
private void startTimer() {
long startTime = firstPlayerRemainingTime;
btnFirstPlayer.setTag(2); // stop timer
btnFirstPlayer.setText("Stop");
mCountDownTimer = new CountDownTimer(firstPlayerRemainingTime, 1000) {
public void onTick(long millisUntilFinished) {
firstPlayerRemainingTime = millisUntilFinished;
tvPlayerOneTimer.setText("" + firstPlayerRemainingTime / 1000)
// Here you would like to check if 30 seconds has passed
if ((startTime / 1000) - (limitedTime / 1000)
== (firstPlayerRemainingTime / 1000)) {
stopTimer();
}
// Here you would like to increase the time of the second player
secondPlayerRemainingTime = ++1000;
}
public void onFinish() {
mTextField.setText("done!");
}
}.start();
}
}
private void stopTimer() {
btnFirstPlayer.setTag(1);
btnFirstPlayer.setText("Start");
mCountDownTimer.cancel();
// I guess here starts second player move
}
The same logic would go for second player. Let me know if this helped you or if I need to explain anything.
I've got an Integer ArrayList 'timerList' (eg - 10, 20, 10) of values which I then want to use for a Countdown Timer.
I'm looking to loop through the list and restart the timer once it finishes so that it uses the next value from the list, so it counts down from 10 and then resets to 20 and then resets to 10 until all values in the list have been used.
The part i'm struggling with is looping through the values of the ArrayList. I can set the initial value from the ArrayList and I'm then trying to use the array list within the onFinish() to set the next time.
I've tried creating an int variable to keep track of where abouts I am in the list and then adding 1 to this every time, to get the next list value but once it has counted down once, it just sticks at 0
Any advice or examples of how I can implement this correctly (perhaps even looking at it from a different perspective if I'm not heading in the right direction) would be greatly appreciated!
Thanks in advance
Paul
I solved this problem by creating array of countdown timers
public class MainActivity extends AppCompatActivity {
CountDownTimer[] countDownTimers;
int Time;
TextView text;
ArrayList<Integer> timeList;
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text= (TextView) findViewById(R.id.text);
timeList=new ArrayList<>();
timeList.add(10*1000);
timeList.add(20*1000);
timeList.add(30*1000);
countDownTimers=new CountDownTimer[timeList.size()];
for(int i=0;i<timeList.size();i++){
final int finalI = i;
countDownTimers[i]=new CountDownTimer(timeList.get(finalI),100) {
#Override
public void onTick(long millisUntilFinished) {
long ms = millisUntilFinished;
String texts = String.format("%02d : %02d",
TimeUnit.MILLISECONDS.toMinutes(ms) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(ms)),
TimeUnit.MILLISECONDS.toSeconds(ms) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(ms)));
text.setText(texts);
}
#Override
public void onFinish() {
if(!((finalI +1)>=timeList.size())){
countDownTimers[finalI+1].start();
}
}
};
}
countDownTimers[0].start();
}
}
I just recently started playing around with Java/Android.
I am trying to make a very simple Android app. I want a number to be progressively increased. Something like:
int x = 0;
then every 0.1 seconds, x++. Then I can set the text of a textview
.setText(String.valueOf(x));
So in the program it will have an integer number increasing from 0 by 1 every 0.1 seconds.
Meanwhile all other functions/code should run normally while this is happening in the background
you could use the TextView's internal handler and its postDelayed method, to increment the int . E.g.
textView.postDelayed(new Runnable() {
#Override
public void run() {
textView.setText(String.valueOf(++x));
textView.postDelayed(this, 100);
}
}, 100);
where 100 is 100 milliseconds. Don't forget to call
textView.removeCallbacks(null);
when your activity is paused
you can use:
int x = 0;
TimerTask scanTask;
Timer t = new Timer();
public void incrementValue(){
scanTask = new TimerTask() {
public void run() {
x++;
}};
t.schedule(scanTask, 100, 100);
}
and use t.cancel(); whenever you want to stop the task.
Why is this Code not working ?
This code should extend the Countdowntimer to 10 sec (everytime when point is 20 , 40 , 60) etc.But when I start it and my score is by 20 it shows in a TextView a right value of time.But then it is gone in 1 sec and the Countdowntimer got the old value and continue.Someone a idea ?
int bonus_time = 1 , sec = 10000 , point == 0;
points_timer =new CountDownTimer(sec,1000) {
#Override
public void onTick(long millisUntilFinished)
{
if ( point == (bonus_time * 20))
{
++bonus_time;
millisUntilFinished += 10000;
sec += 10000;
}
++point;
}
#Override
public void onFinish()
{
bonus_time = 1;
}
};
When you create a timer with the sec variable for its time, it does not magically connect the two so when the value of sec changes the timer's time changes, it just uses the value of sec once when you make the timer.
Same goes for changing millisUntilFinished, it's a parameter you got from a callback, by changing its value you are not doing anything.
The only way to change a timer's time is to create a new one. This is my suggestion:
// GLOBAL VARIABLES
CountDownTimer points_timer;
int bonus_time = 1 , sec = 10000 , point == 0;
public void createTimer()
{
points_timer =new CountDownTimer(sec,1000) {
#Override
public void onTick(long millisUntilFinished)
{
sec = millisUntilFinished;
if ( point == (bonus_time * 20))
{
++bonus_time;
//millisUntilFinished += 10000;
sec += 10000;
createTimer();
}
++point;
}
#Override
public void onFinish()
{
bonus_time = 1;
}
};
}
The creation of the timer is surrounded by a function, and that function is called each time the bonus is gained to recreate the timer with the new value of sec.
You should also call that method once in place of your previous code, to initially create the timer.
Hope this works..
There is no way to extend a CountDownTimer, no method in the class provides this.
Your attempt to add time to sec is useless, I even wonder how it can compile. Anyway, in Java integers are passed by value, so once you passed 1000, you passed it and can't change that value.
The only alernative is to re-create a new timer and use it instead of the old one. Or recode everything, a timer of your own that you can extend.
in
public void onTick(long millisUntilFinished)
you do
millisUntilFinished += 10000;
you can not assign to primitive parameter and expect that it changes anything outside of that method (eg. scope of millisUntilFinished is onTick method and nothing more)
I've looked everywhere for an answer but cannot find one for my situation. I have a couple problems and also wonder how to include millisecond countdown as well. I'm trying to get a countdown timer in the format 00.00 (seconds.milliseconds). A button is used to start the timer. The times I use depend on the button pressed, 5, 10, 15, 30, or 90 seconds. I'll just says its hard coded to 5000 ms to make simpler for now.
long timeSecs = 5000; // really timeSecs is dynamic but for the sake of simplicity
long countDownInterval = 1000; // this is a static value
TextView TVcountDown = (TextView)findViewById(R.id.TVcountDown);
public void createTimer() {
new CountDownTimer(timeSecs, countDownInterval) {
public void onTick(long millisUntilFinished) {
TVcountDown.setText(millisUntilFinished / 1000); // error here on
//.setText unless I cast to an int, which all values are long so I'm not sure why
}
#Override
public void onFinish() {
TVcountDown.setBackgroundColor(R.color.solid_red); // error here
TVcountDown.setTextColor(R.color.white); // error here
TVcountDown.setText("Expired"); // it will make it here
// It doesn't count down, just goes straight to onFinish() and displays "Expired"
}
}.start();
}
Thanks in advance. I've been beating my head against the desk for awhile now.
Try this.
For setText
TVcountDown.setText("" + (millisUntilFinished / 1000));
For the color
Resources res = getResources();
TVcountDown.setBackgroundColor(res.getcolor(R.color.solid_red));
TVcountDown.setTextColor(res.getcolor(R.color.white));
you should get color from the color resource before setting.