0

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:

  1. Call startTimer() in onCreateView of fragment.
  2. In startTimer() define a CountDownTimer class where onTick method reduces progress of a ProgressBar and onFinish shows a Toast message.
  3. 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>
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
Ruturaj Patil
  • 608
  • 1
  • 10
  • 25
  • post a layout xml file where your `R.id.countDown` is defined – pskink May 07 '16 at 05:56
  • add `style=""?android:attr/progressBarStyleHorizontal"` or similar in your `` tag – pskink May 07 '16 at 07:28
  • yeah surprisingly that made it work. although it shows a horizontal progress bar. since I'd not mentioned that in question, you can add your answer and I'll accept it. for making a circular progress bar i used answer here: http://stackoverflow.com/a/27269329/2115117 (and eventually not used countdowntimer at all) – Ruturaj Patil May 07 '16 at 08:58

1 Answers1

0

mistake was missing the style attribute for ProgressBar defined in XML

style=""?android:attr/progressBarStyleHorizontal"

Thanks to pskink's comment

Ruturaj Patil
  • 608
  • 1
  • 10
  • 25