Thursday 25 August 2016

Show Count Down Timer Android



Android Count Down Timer


Hello world. As i promise about my post that i wanna share about android programming, so today i am gonna show you how to show count down timer on android. Ahaa good, its something like timer in your clock app, but in this post i will show you count down. Its mean timer will be show from top to bottom. Lets do this.

On this case i just one have activity and one layout. This is simple you can learn on this page android developer. this is activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvTimeCount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="00:00"
            android:textSize="40dp"
            android:layout_marginTop="30dp"/>

    </LinearLayout>

</LinearLayout>

i got conclusion like this :

1.  android:text="00:00" on my text_view, I can set text string " 00 : 00".
2.  android:textSize="40dp" and then i can set size 40dp.
3.  android:layout_marginTop="30dp" i can set margin top 30dp.
4. and the last this is really important is android:id="@+id/tvTimeCount" . i've id that will call on my java code.

And this one is my main_activity.java .

public class MainActivity extends AppCompatActivity {

    private TextView textViewShowTime;
    private CountDownTimer countDownTimer;
    private long totalTimeCountInMilliseconds;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
        startTimer(2);
    }

    private void startTimer(int time) {
        totalTimeCountInMilliseconds = 60 * time * 1000;
        totalTimeCountInMilliseconds = totalTimeCountInMilliseconds + 1000;
        countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 1000) {

            @Override
            public void onTick(long leftTimeInMilliseconds) {
                long seconds = leftTimeInMilliseconds / 1000;
                String minute =  String.format("%02d", seconds / 60);
                String second =  String.format("%02d", seconds % 60);
                textViewShowTime.setText(minute+" : "+ second);
            }

            @Override
            public void onFinish() {
                textViewShowTime.setText("Time up!");
            }
        }.start();
    }
}

i got conclusion that :

1. Declaration your text_view with (TextView) findViewById(R.id.tvTimeCount);
2. Call my function, i set 2. its mean 2 minutes. startTimer(2);
3. totalTimeCountInMilliseconds = 60 * time * 1000; Calculate int to minutes. in android 1 second is 1000 millisecond. Remember!!
4. Call basic function CountDownTimer with parameter our minutes and speed (1000 millisecond normal ).
5. Create logic to get minutes and seconds. with divide and modulus ( / and % ).
6. Show your minutes and seconds with little modification.
    textViewShowTime.setText(minute+" : "+ second);
7. Funtion onTick() is time still on workingm but deferent with onFinish() is time is stop. So on this case when time still working my text view, i put time and then when stop i put string "time is up".

Yup this is my little trick about timer on android, i hope this is can help you little bit. And also you can download on my github link. Thanks.

Github link
Share:
Lokasi: Cilandak, South Jakarta City, Special Capital Region of Jakarta, Indonesia

1 comment:

  1. Graton Hotel Casino & Spa - Mapyro
    Find the 상주 출장안마 best 시흥 출장안마 value on Graton Hotel septcasino Casino & 서귀포 출장안마 Spa in St. 서귀포 출장마사지 Louis and other St. Louis metropolitan areas. We offer fast payouts so you can save!

    ReplyDelete


<br>


Advice for contacting me at dedyy54@gmail.com