Example usage for android.os CountDownTimer CountDownTimer

List of usage examples for android.os CountDownTimer CountDownTimer

Introduction

In this page you can find the example usage for android.os CountDownTimer CountDownTimer.

Prototype

public CountDownTimer(long millisInFuture, long countDownInterval) 

Source Link

Usage

From source file:adventure_fragments.Skiing.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.skiing_fragment_layout, container, false);
    this.rootView = rootView;
    db = new DataBaseHelper(getActivity());
    list = db.getAllItemsSkiing();//w ww  .j a  va2s . c om

    adapt = new CustomAdapter5(getActivity(), R.layout.item_layout, list);
    listItem = (ListView) rootView.findViewById(R.id.listview_ski);
    listItem.setAdapter(adapt);
    nothingtext = (TextView) rootView.findViewById(R.id.nothing_here_ski);
    count = listItem.getCount();
    if (count > 0) {
        nothingtext.setVisibility(View.GONE);
    } else if (count == 0) {
        nothingtext.setVisibility(View.VISIBLE);
    }
    final ImageButton imageButton = (ImageButton) rootView.findViewById(R.id.add_item_skiing);
    listItem.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            switch (scrollState) {
            case 2: // SCROLL_STATE_FLING
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 1: // SCROLL_STATE_TOUCH_SCROLL
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 0: // SCROLL_STATE_IDLE
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;

            default:
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;
            }
        }
    });

    imageButton.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            CountDownTimer timer = new CountDownTimer(4000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    imageButton.setVisibility(View.GONE);
                }

                @Override
                public void onFinish() {
                    imageButton.setVisibility(View.VISIBLE);
                }

            };

            timer.start();

            return true;
        }
    });

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddItemNow();
            AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
            animation1.setDuration(500);
            imageButton.startAnimation(animation1);

        }
    });

    return rootView;

}

From source file:adventure_fragments.Kayaking.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.kayaking_fragment, container, false);
    this.rootView = rootView;
    db = new DataBaseHelper(getActivity());
    list = db.getAllItemsKayaking();/*ww w.ja v a 2s  .c  o  m*/

    adapt = new CustomAdapter3(getActivity(), R.layout.item_layout, list);
    listItem = (ListView) rootView.findViewById(R.id.listview_kayak);
    listItem.setAdapter(adapt);
    nothingtext = (TextView) rootView.findViewById(R.id.nothing_here_kayak);
    count = listItem.getCount();
    if (count > 0) {
        nothingtext.setVisibility(View.GONE);
    } else if (count == 0) {
        nothingtext.setVisibility(View.VISIBLE);
    }

    final ImageButton imageButton = (ImageButton) rootView.findViewById(R.id.add_item_kayaking);
    listItem.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            switch (scrollState) {
            case 2: // SCROLL_STATE_FLING
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 1: // SCROLL_STATE_TOUCH_SCROLL
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 0: // SCROLL_STATE_IDLE
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;

            default:
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;
            }
        }
    });

    imageButton.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            CountDownTimer timer = new CountDownTimer(4000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    imageButton.setVisibility(View.GONE);
                }

                @Override
                public void onFinish() {
                    imageButton.setVisibility(View.VISIBLE);
                }

            };

            timer.start();

            return true;
        }
    });

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddItemNow();
            AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
            animation1.setDuration(500);
            imageButton.startAnimation(animation1);

        }
    });

    return rootView;

}

From source file:adventure_fragments.Swimming.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.swimming_fragment_layout, container, false);
    this.rootView = rootView;
    db = new DataBaseHelper(getActivity());
    list = db.getAllItemsSwimming();//  w w w . j a va 2s .c o m

    adapt = new CustomAdapter6(getActivity(), R.layout.item_layout, list);
    listItem = (ListView) rootView.findViewById(R.id.listview_swim);
    listItem.setAdapter(adapt);
    nothingtext = (TextView) rootView.findViewById(R.id.nothing_here_swim);
    count = listItem.getCount();
    if (count > 0) {
        nothingtext.setVisibility(View.GONE);
    } else if (count == 0) {
        nothingtext.setVisibility(View.VISIBLE);
    }
    final ImageButton imageButton = (ImageButton) rootView.findViewById(R.id.add_item_swimming);
    listItem.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            switch (scrollState) {
            case 2: // SCROLL_STATE_FLING
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 1: // SCROLL_STATE_TOUCH_SCROLL
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 0: // SCROLL_STATE_IDLE
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;

            default:
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;
            }
        }
    });

    imageButton.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            CountDownTimer timer = new CountDownTimer(4000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    imageButton.setVisibility(View.GONE);
                }

                @Override
                public void onFinish() {
                    imageButton.setVisibility(View.VISIBLE);
                }

            };

            timer.start();

            return true;
        }
    });

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddItemNow();
            AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
            animation1.setDuration(500);
            imageButton.startAnimation(animation1);

        }
    });

    return rootView;

}

From source file:adventure_fragments.Camping.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.camping_fragment_layout, container, false);
    this.rootView = rootView;
    db = new DataBaseHelper(getActivity());
    list = db.getAllItemsCamping();//from   w w  w . j a  va  2 s . c om

    adapt = new CustomAdapter1(getActivity(), R.layout.item_layout, list);
    listItem = (ListView) rootView.findViewById(R.id.listview_camp);
    listItem.setAdapter(adapt);
    nothingtext = (TextView) rootView.findViewById(R.id.nothing_here_camp);
    count = listItem.getCount();
    if (count > 0) {
        nothingtext.setVisibility(View.GONE);
    } else if (count == 0) {
        nothingtext.setVisibility(View.VISIBLE);
    }

    final ImageButton imageButton = (ImageButton) rootView.findViewById(R.id.add_object_camping);
    listItem.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            switch (scrollState) {
            case 2: // SCROLL_STATE_FLING
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 1: // SCROLL_STATE_TOUCH_SCROLL
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 0: // SCROLL_STATE_IDLE
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;

            default:
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;
            }
        }
    });

    imageButton.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            CountDownTimer timer = new CountDownTimer(4000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    imageButton.setVisibility(View.GONE);
                }

                @Override
                public void onFinish() {
                    imageButton.setVisibility(View.VISIBLE);
                }

            };

            timer.start();

            return true;
        }
    });

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddItemNow();
            AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
            animation1.setDuration(500);
            imageButton.startAnimation(animation1);

        }
    });

    return rootView;

}

From source file:adventure_fragments.Climbing.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.climbing_fragment_layout, container, false);
    this.rootView = rootView;
    db = new DataBaseHelper(getActivity());
    list = db.getAllItemsClimbing();/*from w  w  w  . j  a va 2 s  . c  o  m*/

    adapt = new CustomAdapter1(getActivity(), R.layout.item_layout, list);
    listItem = (ListView) rootView.findViewById(R.id.listview_climb);
    listItem.setAdapter(adapt);
    nothingtext = (TextView) rootView.findViewById(R.id.nothing_here_climb);
    count = listItem.getCount();
    if (count > 0) {
        nothingtext.setVisibility(View.GONE);
    } else if (count == 0) {
        nothingtext.setVisibility(View.VISIBLE);
    }

    final ImageButton imageButton = (ImageButton) rootView.findViewById(R.id.climb_button);
    listItem.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            switch (scrollState) {
            case 2: // SCROLL_STATE_FLING
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 1: // SCROLL_STATE_TOUCH_SCROLL
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 0: // SCROLL_STATE_IDLE
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;

            default:
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;
            }
        }
    });

    imageButton.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            CountDownTimer timer = new CountDownTimer(4000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    imageButton.setVisibility(View.GONE);
                }

                @Override
                public void onFinish() {
                    imageButton.setVisibility(View.VISIBLE);
                }

            };

            timer.start();

            return true;
        }
    });

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddItemNow();
            AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
            animation1.setDuration(500);
            imageButton.startAnimation(animation1);

        }
    });

    return rootView;

}

From source file:adventure_fragments.Biking.java

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
        @Nullable Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.biking_fragment_layout, container, false);

    this.rootView = rootView;
    db = new DataBaseHelper(getActivity());
    list = db.getAllItemsBiking();/*from   w w  w.  jav  a2  s . co  m*/
    listItem = (ListView) rootView.findViewById(R.id.listview_bike);
    adapt = new CustomAdapter(getActivity(), R.layout.item_layout, list);
    listItem.setAdapter(adapt);
    nothingtext = (TextView) rootView.findViewById(R.id.nothing_here_bike);
    count = listItem.getCount();
    if (count > 0) {
        nothingtext.setVisibility(View.GONE);
    } else if (count == 0) {
        nothingtext.setVisibility(View.VISIBLE);
    }
    Log.i("Count = ", "" + count);

    final ImageButton imageButton = (ImageButton) rootView.findViewById(R.id.refresh);

    listItem.setOnScrollListener(new AbsListView.OnScrollListener() {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            switch (scrollState) {
            case 2: // SCROLL_STATE_FLING
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 1: // SCROLL_STATE_TOUCH_SCROLL
                //hide button here
                imageButton.setVisibility(View.GONE);
                break;

            case 0: // SCROLL_STATE_IDLE
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;

            default:
                //show button here
                imageButton.setVisibility(View.VISIBLE);
                break;
            }
        }
    });

    imageButton.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            CountDownTimer timer = new CountDownTimer(4000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    imageButton.setVisibility(View.GONE);
                }

                @Override
                public void onFinish() {
                    imageButton.setVisibility(View.VISIBLE);
                }

            };

            timer.start();

            return true;
        }
    });

    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AddItemNow();
            AlphaAnimation animation1 = new AlphaAnimation(0.2f, 1.0f);
            animation1.setDuration(500);
            imageButton.startAnimation(animation1);

        }
    });

    return rootView;

}

From source file:net.potterpcs.recipebook.TimerFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    startButton = (Button) getActivity().findViewById(R.id.timerstartbutton);
    stopButton = (Button) getActivity().findViewById(R.id.timerstopbutton);
    display = (TextView) getActivity().findViewById(R.id.timerdisplay);
    minutePicker = (NumberPicker) getActivity().findViewById(R.id.minutepicker);
    secondPicker = (NumberPicker) getActivity().findViewById(R.id.secondpicker);
    timermin = (EditText) getActivity().findViewById(R.id.timerdisplayminutes);
    timersec = (EditText) getActivity().findViewById(R.id.timerdisplayseconds);

    if (minutePicker != null) {
        minutePicker.setMinValue(0);/*ww w. j a va 2  s.c o  m*/
        minutePicker.setMaxValue(99);
        secondPicker.setMinValue(0);
        secondPicker.setMaxValue(59);
        secondPicker.setFormatter(new Formatter() {
            @Override
            public String format(int value) {
                return String.format("%02d", value);
            }
        });
        getActivity().findViewById(R.id.pickerlayout).setVisibility(View.VISIBLE);
        display.setVisibility(View.GONE);
    }

    startButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            int seconds;
            if (minutePicker != null) {
                seconds = minutePicker.getValue() * 60 + secondPicker.getValue();
            } else {
                int m = Integer.parseInt(timermin.getEditableText().toString());
                int s = Integer.parseInt(timersec.getEditableText().toString());
                seconds = m * 60 + s;
                timermin.setText(timermin.getText(), BufferType.NORMAL);
                timersec.setText(timersec.getText(), BufferType.NORMAL);
            }
            startButton.setEnabled(false);
            stopButton.setEnabled(true);

            if (minutePicker == null) {
                timermin.setEnabled(false);
                timersec.setEnabled(false);
            } else {
                minutePicker.setEnabled(false);
                secondPicker.setEnabled(false);
                getActivity().findViewById(R.id.pickerlayout).setVisibility(View.GONE);
                display.setVisibility(View.VISIBLE);
            }

            if (display != null) {
                display.setText(DateUtils.formatElapsedTime(seconds));
            }

            timer = new CountDownTimer(seconds * 1000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    //                  Log.i(TAG, "tick " + millisUntilFinished);
                    if (display != null) {
                        display.setText(DateUtils.formatElapsedTime(millisUntilFinished / 1000));
                    } else {
                        int totalsec = (int) (millisUntilFinished / 1000);
                        int s = totalsec % 60;
                        int m = totalsec / 60;
                        timermin.setText(String.format("%02d", m));
                        timersec.setText(String.format("%02d", s));
                    }
                }

                @Override
                public void onFinish() {
                    // Play a sound and show a message when the timer ends
                    MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.ding);
                    mp.setOnCompletionListener(new OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            mp.release();
                        }
                    });
                    mp.setVolume(1.0f, 1.0f);
                    Toast.makeText(getActivity(), "Done!", Toast.LENGTH_LONG).show();
                    mp.start();
                    clearToZero();
                }
            };

            timer.start();
        }
    });

    stopButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            timer.cancel();
            clearToZero();
        }
    });
}

From source file:capstone.se491_phm.Alarm.java

public static CountDownTimer showTimer(final Context context, long countDownFrom) {
    return new CountDownTimer(countDownFrom, 1000) {
        public void onTick(long millisUntilFinished) {
            if (!mUserAckAlarm) {
                String display = String.format("%02d min, %02d sec",
                        TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES
                                .toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
                FallDetectedActivity.setCountDownTextValue(display);
            } else {
                cancelTimer();//from ww w  .ja  va  2  s.  c om
            }
        }

        public void onFinish() {
            if (!mUserAckAlarm) {
                mSmsEscalation = true;
            }
            this.cancel();
        }
    }.start();
}

From source file:com.tatteam.patente.ui.fragment.DoExamsFragment.java

private void startTimer() {
    countDown = examTotalDuration + 1;/* ww w . java  2 s  .  c om*/
    timer = new CountDownTimer(Integer.MAX_VALUE, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            countDown--;
            if (countDown > 0) {
                int min = countDown / 60;
                int second = countDown - (min * 60);
                textViewMin.setText(StringUtil.formatNumber(min));
                textViewSecond.setText(StringUtil.formatNumber(second));
                if (countDown >= EXAMS_WARNING_TIME) {
                    textViewMin.setTextColor(getResources().getColor(R.color.do_exam_min_normal));
                } else {
                    textViewMin.setTextColor(getResources().getColor(R.color.do_exam_min_warning));
                }
            } else {
                timer.cancel();
                switchToCorreggiFragment();
            }
        }

        @Override
        public void onFinish() {

        }
    };
    timer.start();
}

From source file:com.microsoft.mimickeralarm.mimics.CountDownTimerView.java

private void createNewTimer(long millisUntilFinished) {
    mTimer = new CountDownTimer(millisUntilFinished, sInterval) {
        @Override/*  w ww  .  ja v  a  2 s.  c  om*/
        public void onTick(long millisUntilFinished) {
            mMillisUntilFinished = millisUntilFinished;
            invalidate();
        }

        @Override
        public void onFinish() {
            if (mCommand != null) {
                mMillisUntilFinished = 0;
                invalidate();
                mCommand.execute();
            }
        }
    };
}