'get one time start and stop time in android studio java language
I have a button that show start time in textview on click then turn into text of button to stop and when again click on same button it show stop time and make difference between two time. my problem is that it take multiple time i means when i click again and again it show again new start and stop time. i want that once i get start time by clicking on button then get stop time and thats time shouldnt be change by again clicking on button untill it will save into database and clear the textboxes.
public class Aircraft extends Fragment {
TextView tvstarttime;
Button btn_start;
Button btn_end;
TextView tvend;
TextView diffence;
boolean isStarted = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_aircraft, container, false);
tvstarttime = view.findViewById(R.id.txtstarttime);
btn_start = view.findViewById(R.id.btn_start);
tvend = view.findViewById(R.id.txtendtime);
diffence=view.findViewById(R.id.txtdifference);
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isStarted){
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
String time = format.format(calendar.getTime());
tvend.setText(time);
try {
Date date1 = format.parse(tvend.getText().toString());
Date date2 = format.parse(tvstarttime.getText().toString());
long mills = date1.getTime() - date2.getTime();
Log.v("Data1", ""+date1.getTime());
Log.v("Data2", ""+date2.getTime());
int hours = (int) (mills/(1000 * 60 * 60));
int mins = (int) (mills % (1000*60*60));
String diff = hours + ":" + mins;
diffence.setText(diff);
} catch (ParseException e) {
e.printStackTrace();
}
btn_start.setText("START");
}else {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat format = new SimpleDateFormat("hh:mm:ss aa");
String time1 = format.format(calendar.getTime());
tvstarttime.setText(time1);
btn_start.setText("STOP");
}
isStarted = !isStarted;
}
});
return view;
}
}
Solution 1:[1]
I would use a thread to calculate to manage the timer and and a thread to run on user interface (runOnUiThread) to print the difference ino the textview by using a while or for loop. Like
while (isstarted= true) {
TV.settext(difference);
}
So the key solution for this task is to provide a Code that doesnt block your main code in Java and allows further operation on the user interface (Clicklisteners, Touchlisteners...).
For this Purpose Threads are used.
And the second thing is to use a loop to keep updating the textview with the timedifference.
And you wont have to worry about blocking your main code with the loop since you are working with threads which run parallel to the main Program and not in series.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | abdussamed17 |
