I've to just show a count down fragment at certain trigger which includes a progress bar that reduces count on tick of a countdown timer.
I'm using following simple steps:
- Call
startTimer()
inonCreateView
of fragment. - In
startTimer()
define aCountDownTimer
class whereonTick
method reduces progress of aProgressBar
andonFinish
shows a Toast message. - Start this timer on UI thread using
getActivity().runOnUIThread()
Problem is the timer executes but ProgressBar
keeps showing indeterminate progress i.e. it remains unchanged. Toast is also seen at the finish but nothing changes for progress bar.
What's going wrong here?
Below is the current code:
public class IncomingRequestFragment extends Fragment {
private OnFragmentInteractionListener mListener;
private FrameLayout root;
private CountDownTimer mCountDownTimer;
private ProgressBar mProgressBar;
public IncomingRequestFragment() {
// Required empty public constructor
}
public static IncomingRequestFragment newInstance(String param1, String param2) {
IncomingRequestFragment fragment = new IncomingRequestFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
int progress=25;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
root = (FrameLayout) inflater.inflate(R.layout.fragment_incoming_request, container, false);
mProgressBar = (ProgressBar) root.findViewById(R.id.countDown);
progress = mProgressBar.getMax();
mProgressBar.setIndeterminate(false);
mProgressBar.setProgress(progress);
startTimer();
return root;
}
private void startTimer() {
mCountDownTimer = new CountDownTimer(25000,1000) {
@Override
public void onTick(long millisUntilFinished) {
mProgressBar.setProgress(progress--);
Log.d("count",String.valueOf(progress));
}
@Override
public void onFinish() {
Toast.makeText(getContext(),"You just missed a trip!",Toast.LENGTH_SHORT).show();
}
};
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mCountDownTimer.start();
}
});
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Layout XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.taxiwaxidriver.ui.fragments.IncomingRequestFragment">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/countDown"
android:layout_margin="60dp"
android:max="25"
android:progressTint="@android:color/holo_blue_dark"
android:progressBackgroundTint="@android:color/holo_blue_dark"
android:progress="25"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="12dp"
android:weightSum="2"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/white"
android:text="REJECT"
android:id="@+id/rideLater"
android:background="@drawable/rounded_button_left"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ACCEPT"
android:textColor="@color/white"
android:background="@drawable/rounded_button_right"
android:id="@+id/rideNow"/>
</LinearLayout>
</FrameLayout>
Output of above code is:
05-07 10:49:05.623 21455-21455 D/count: 24
05-07 10:49:06.626 21455-21455 D/count: 23
05-07 10:49:07.636 21455-21455 D/count: 22
05-07 10:49:08.653 21455-21455 D/count: 21
.
.
.
05-07 10:49:28.917 21455-21455 D/count: 1<br>