I have two SeekBar in my application. One shows hours and the other progresses in interval of 15 minutes (0,15,30,45).
I have a initial value of time set to 10.30. Iam displaying the changed time in a TextView.
Now, if the user only increments the minute seekbar (eg 15 mins) the textView should show 10.45. (ie. 10.30 + 00:15). Similarly, if the user further increments the minute seekbar to 30 or 45 the TextView should show 11:00 and 11:15 respectively.
Currently, Everything works fine until the minutes goes upto 15 minutes. But when I increase the SeekBar to 30 or 45 the TextView shows 10:60 and 10:75 instead.
Here is my code:-
int hourLimit=10, minLimit=30;
seekBarHour.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar)
{
if(progressChangedHours == 0 && progressChangedMins==0)
{
txtReachingBy.setText("Reaching by:"+" "+String.valueOf(hourLimit)+":"+String.valueOf(minLimit)+":"+today.second);
}
if(progressChangedHours==0 && mins >=60)
{
hour = 1;
}
else
{
hour = hourLimit + progressChangedHours;
}
timeString = String.valueOf(hour)+":"+String.valueOf(mins)+":"+today.second;
txtReachingBy.setText("Reaching by:"+" "+timeString);
}
});
seekBarMins.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
progressChangedMins = progress;
int stepSize = 15;
progressChangedMins = (progressChangedMins/stepSize)*stepSize;
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar)
{
if(progressChangedMins == 0)
{
txtReachingBy.setText("Reaching by:"+" "+String.valueOf(hourLimit)+":"+String.valueOf(30)+":"+today.second);
}
else
{
mins = minLimit +progressChangedMins;
if(progressChangedHours==0 && mins!=0)
{
hour = hourLimit;
}
else if(progressChangedHours>=0 && mins>=60)
{
hour = hour+1;
mins = mins-60;
}
timeString = String.valueOf(hour)+":"+String.valueOf(mins)+":"+today.second;
txtReachingBy.setText("Reaching by:"+" "+timeString);
}
}
});
EDIT
: I used the Date to solve a part of my problem like so:-
DateFormat sdf = new SimpleDateFormat("hh:mm:ss");
Date date;
try
{
date = sdf.parse(initialTime);
cal= Calendar.getInstance();
cal.setTime(date);
txtReachingBy.setText("Reaching By:"+" "+ cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
seekBarMins.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
progressChangedMins = progress;
int stepSize = 15;
progressChangedMins = (progressChangedMins/stepSize)*stepSize;
seekBar.setProgress(progressChangedMins);
if(progressChangedHours ==0 && progressChangedMins!=0)
{
lblLateBy.setText("Late By (Hrs):"+" " + String.valueOf(progressChangedHours) +":"+String.valueOf(progressChangedMins));
}
else
lblLateBy.setText("Late By (Hrs):"+" " + String.valueOf(progressChangedHours) +":"+String.valueOf(progressChangedMins));
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar)
{
if(progressChangedMins==0)
{
timeString = cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+today.second;
}
else
{
cal.add(Calendar.MINUTE, progressChangedMins);
timeString = cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+":"+today.second;
}
txtReachingBy.setText("Reaching by:"+" "+timeString);
}
});
Now everything works fine if I drag the seek bar thumb at once to a value. The progressed value gets added to the initial time. But if I drag it to a value the initial time changes with the addition, then again if I drag it further the progressed value gets added to the new time and not the initial one.
Giving me the output as:-
Drag 1-> 15 mins > Time expected 10.45 (Fine)
Drag 2-> 30 mins > Time expected 11.00 Output I get -> 10.45+00.30 = 11.15
How can I solve this?
Related
I have following code in my service.
countDownTimer = new CountDownTimer(60000 * 3, 1000) {
#Override
public void onTick(long l) {
intentDSU.putExtra("remaind", TimeUnit.MILLISECONDS.toSeconds(l));
sendBroadcast(intentDSU);
}
#Override
public void onFinish() {
postHandler();
intentDSU.putExtra("remaind", 0);
sendBroadcast(intentDSU);
countDownTimer.start();
}
};
countDownTimer.start();
That's work fine, but when i try convert seconds to minutes and remaind of seconds on my activity
private void updateGUI(Intent intent){
if(intent.getExtras() != null){
long remaind = intent.getLongExtra("remaind", 0);
if(remaind == 0){
onRefresh();
}
long minutes = TimeUnit.SECONDS.toMinutes(remaind) - (TimeUnit.SECONDS.toHours(remaind)* 60);
long seconds = TimeUnit.SECONDS.toSeconds(remaind) - (TimeUnit.SECONDS.toMinutes(remaind) *60);
MenuItem menuItem = menu.findItem(R.id.action_timer);
if(menuItem != null){
if(minutes > 0){
menuItem.setTitle(String.format("%o min.", minutes));
}
else {
menuItem.setTitle(String.format("%o sec.", seconds));
}
}
}
}
I become notice unusual results, seconds will start 73 instead 60. I haven't idea, what's wrong?
you're code is correct, I quess you get problem in String.format("%o min.", minutes) and here String.format("%o sec.", seconds).
Try to use this.
MenuItem menuItem = menu.findItem(R.id.action_timer);
if(menuItem != null){
if(minutes > 0){
menuItem.setTitle(String.format("%s min.", Long.toString(minutes)));
}
else {
menuItem.setTitle(String.format("%s sec.", Long.toString(seconds)));
}
}
try this
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
long sec=TimeUnit.MILLISECONDS.toSeconds(passmillitimer);
In my application i should use CountDownTimer and for this I want use this library : CountdownView.
I want when lasted 5s show me Toast.
For example : I want show 17s countTimer, when this timer receive to 5s show me toast "just 5s".
I use this code, but show me ever 5s .
long time1 = 17 * 1000;
mCvCountdownViewTest1.start(time1);
mCvCountdownViewTest1.setOnCountdownIntervalListener(5000, new CountdownView.OnCountdownIntervalListener() {
#Override
public void onInterval(CountdownView cv, long remainTime) {
Toast.makeText(MainActivity.this, "Just 5s ", Toast.LENGTH_SHORT).show();
}
});
I want just lasted 5s not ever 5s.
how can i it?
By modifying your code:
boolean toastShown = false;
long time1 = 17 * 1000;
mCvCountdownViewTest1.start(time1);
mCvCountdownViewTest1.setOnCountdownIntervalListener(1000, new CountdownView.OnCountdownIntervalListener() {
#Override
public void onInterval(CountdownView cv, long remainTime) {
if(remainTime < 5000 && !toastShown) {
toastShown = true;
Toast.makeText(MainActivity.this, "Just 5s ", Toast.LENGTH_SHORT).show();
}
}
});
You can try this code also:
CountDownTimer countDownTimer = new CountDownTimer(17000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
Toast.makeText(HomeActivity.this, "running" + millisUntilFinished, Toast.LENGTH_SHORT).show();
if (millisUntilFinished < 5000) {
Toast.makeText(HomeActivity.this, "5 sec left", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFinish() {
Toast.makeText(HomeActivity.this, "Countdown Timer Finished", Toast.LENGTH_SHORT).show();
}
};
countDownTimer.start();
I think you should check how many seconds remains.
I rewrote your code:
long time1 = 17L * 1000;
mCvCountdownViewTest1.start(time1);
mCvCountdownViewTest1.setOnCountdownIntervalListener(1000, new CountdownView.OnCountdownIntervalListener() {
#Override
public void onInterval(CountdownView cv, long remainTime) {
long remainTimeInSeconds = remainTime / 1000;
if (remainTimeInSeconds == 5) {
Toast.makeText(MainActivity.this, "Just 5s ", Toast.LENGTH_SHORT).show();
}
}
});
At first I've thought this was a problem with Parse but then I started to log the value of the variable that im sending to the cloud. And for some reason the value is 0. I have a seeker bar which updates the value of a variable when the bar gets slid around. I had a log statement which showed the value changing so I know there is nothing wrong with my code up until button click.
And then with the click of a button, that variable's value is to get sent to Parse. I've logged the value upon button click and the variable contains a value of 0. I have no idea why this happening. I know that the variable has a value right up until the button is clicked. And then somehow the value is manipulated
public class SettingsScreen extends AppCompatActivity {
... //////// ommitted code /////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings_screen);
ButterKnife.bind(this);
Intent intent = getIntent();
isLoggedIn = intent.getBooleanExtra("currentUser", false);
if (isLoggedIn) {
userIsLoggedIn();
} else {
... //////// ommitted code /////
}
applyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isLoggedIn) {
mUser.put("WORK", mDesiredWorkTime);
mUser.put("REST", mDesiredRestTime);
mUser.put(ACCESSEDSETTINGS, true); //when user clicks apply, ACCESSEDSETTINGS will be true and so the if statement in the method below wont run
mUser.saveInBackground();
Log.d(TAG, "putting desired user settings into database");
Log.d(TAG, "desired work time: " + mDesiredWorkTime); // SOMETIMES THIS IS ZERO???
Log.d(TAG, "desired rest time " + mDesiredRestTime); // AND SOMETIMES THIS IS ZERO???
}
Intent intent = new Intent(SettingsScreen.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(LOCALWORK, mDesiredWorkTime);
intent.putExtra(LOCALREST, mDesiredRestTime);
startActivity(intent);
}
});
}
public void userIsLoggedIn() {
mUser = ParseUser.getCurrentUser();
mObjectId = mUser.getObjectId();
registeredUserAccessedSettings = mUser.getBoolean(ACCESSEDSETTINGS);
Log.d(TAG, "Has user accessed settings before? " + registeredUserAccessedSettings);
if (!registeredUserAccessedSettings) { //this code will run first time user accesses settings with parse
workFromDatabase = 25;
restFromDatabase = 5;
Log.d(TAG, "launched settings with account for first time");
} else {
workFromDatabase = mUser.getInt("WORK");
restFromDatabase = mUser.getInt("REST");
workText.setText("Current: " + workFromDatabase + " minutes");
restText.setText("Current: " + restFromDatabase + " minutes");
Log.d(TAG, "got user settings from database");
Log.d(TAG, "desired work time: " + workFromDatabase);
Log.d(TAG, "desired rest time " + restFromDatabase);
}
workBar.setMax(MAX_WORK - MIN_WORK);
workBar.setProgress(workFromDatabase - MIN_WORK);
workBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mDesiredWorkTime = MIN_WORK + progress;
workText.setText("Current: " + mDesiredWorkTime + " minutes");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
restBar.setMax(MAX_REST - MIN_REST);
restBar.setProgress(restFromDatabase - MIN_REST);
restBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mDesiredRestTime = MIN_REST + progress;
restText.setText("Current: " + mDesiredRestTime + " minutes");
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
}
EDIT
I have two seeker bars which are updating two different variables. One of these variables will always have a value while the other variable has a value of 0. The value of 0 alternates between each variable everytime I click the button. This is something I've never seen before
EDIT
mDesiredWorkTime = workFromDatabase;
mDesiredRestTime = restFromDatabase;
placed this right before the if else block and now these variables will always hold a value
Is it possible to implement a fast forward button using the onLongClick button event?
EDIT
i used runnable inside the onlongclicklistner and adding the code for reference who needs :)
Button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
final Runnable r = new Runnable()
{
public void run()
{//do the forwarding logic here
if(Button.isPressed()){
Button.postDelayed(this, 1000); //delayed for 1 sec
}else{
Button.postInvalidate();
Button.invalidate();
}
}
};
Button.post(r);
return true;
}
});
In your onLongClick event, set a member variable (example: mShouldFastForward) to true.
In the rest of your code (perhaps each frame played?) check if mShouldFastForward == true; if so, perform a fast-forward on that frame.
Use an onTouch event to capture the MotionEvent.ACTION_UP to set mShouldFastForward to false.
I have done it in this project (the project is not finished (ie polished) but fast forward works):
https://bitbucket.org/owentech/epileptic-gibbon-android
Take a look at playerfragment.java :
I handle this by using Threads to fast forward the mediaplayer.
Example code from project:
/*******************************/
/* Fast-Forward button actions */
/*******************************/
ffbutton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent event) {
switch (event.getAction() ) {
case MotionEvent.ACTION_DOWN:
arrays.fastforwardpressed = true;
FastForwardThread newFFThread = new FastForwardThread();
arrays.fastforwardfrom = mp.getCurrentPosition();
arrays.fastforwardto = arrays.fastforwardfrom;
newFFThread.start();
break;
case MotionEvent.ACTION_UP:
arrays.fastforwardpressed = false;
mp.seekTo(arrays.fastforwardto);
break;
}
return true;
}
});
public class FastForwardThread extends Thread
{
public FastForwardThread()
{
super("FastForwardThread");
}
public void run()
{
while (arrays.fastforwardpressed == true)
{
arrays.fastforwardto = arrays.fastforwardto + 10000;
int fastforwardseconds = arrays.fastforwardto / 1000;
int hours = fastforwardseconds / 3600, remainder = fastforwardseconds % 3600, minutes = remainder / 60, seconds = remainder % 60;
String Hours = Integer.toString(hours);
String Minutes = Integer.toString(minutes);
String Seconds = Integer.toString(seconds);
if (Hours.length() == 1)
{
Hours = "0" + Hours;
}
if (Minutes.length() == 1)
{
Minutes = "0" + Minutes;
}
if (Seconds.length() == 1)
{
Seconds = "0" + Seconds;
}
arrays.formattedfftime = Hours + ":" + Minutes + ":" + Seconds;
fastforwardHandler.sendEmptyMessage(0);
try
{
sleep(100);
} catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}
How to set previous selected value in TimePicker?
I mean, click on TextView, TimePicker dialog occurs, then I select the time and set it in the TextView, e.g. 12:30 PM on TextView.
After that, if I want to change the time, click again to TextView and then TimePicker dialog should show the previous selected value. So the TimePicker should display 12:30 PM.
How can I do that?
If you create Time Picker Dialog with onCreateDialog and call it it will automatically store the previous value.
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
TimePickerDialog timeDlg = new TimePickerDialog(this,
new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
// TODO Auto-generated method stub
if (hourOfDay > 12) {
hourOfDay = hourOfDay - 12;
time = " PM";
} else if (hourOfDay < 12 && hourOfDay != 0) {
time = " AM";
} else if (hourOfDay == 12) {
time = " PM";
} else if (hourOfDay == 0) {
hourOfDay = 12;
time = " AM";
}
Toast.makeText(
getApplicationContext(),
new StringBuilder().append(pad(hourOfDay))
.append(":").append(pad(minute))
.append(time), Toast.LENGTH_SHORT)
.show();
}
}, 12, 00, false);
timeDlg.setMessage("Set Time:");
timeDlg.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Dismiss",
Toast.LENGTH_SHORT).show();
}
});
return timeDlg;
}
return null;
}
Use showDialog(id); to Show the Dialog.