- Advanced Android - Home
- Advanced Android - Material Design
- Advanced Android - Sliding Menu
- Advanced Android - Tabs
- Advanced Android - Sliders
- Advanced Android - Floating Labels
- Advanced Android - ViewPager
- Material Login & Registration
- Advanced Android - Snackbar
- Expandable List View
- Search Functionality
- Count Down Functionality
- Draw Spritesheet
- Advanced Android - Linkify Class
- Advanced Android - ViewFlipper
- Advanced Android - loader
- Advanced Android - Slider Menu
- SwipeRefreshLayout
- Shake to Refresh
- Advanced Android - Color Palette
- Crumbling Layout
- Advanced Android - Page Flip
- Ken Burns effect
- Advanced Android - Parallax Header
- Borderless Dialog
- Advanced Android - Holder View
- Scrolling TextView by Ticker
- Floating Action Menu
- Spinner Data from Database
- Advanced Android - Flexbox Layout
- Advanced Android - YouTube API
- Video Streaming
- Cardview & Recycleview
Count Down Functionality
Count Down is an act of counting numerals in reverse order to zero, especially before the launching of a rocket or missile.
Example
This example demostrate about how to integrate Android Count Down.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
<LinearLayout 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"
android:orientation = "vertical" >
<Button
android:id = "@+id/startbutton"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:text = "Start"
android:textAlignment = "center" >
</Button>
<TableLayout
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:gravity = "center"
android:padding = "10dp">
<TableRow>
<TextView
android:id = "@+id/timer"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:paddingRight = "10dp"
android:text = "Time: "
android:textSize = "20dp" />
</TableRow>
</TableLayout>
<TextView
android:id = "@+id/timeElapsed"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:paddingLeft = "10dp"
android:paddingRight = "10dp"
android:text = "Time elapsed: "
android:textSize = "20dp" />
</LinearLayout>
Step 3 − Add the following code to src/MainActivity.java
package myapplication.example.com.myapplication;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private santimer timer;
private Boolean timer_flag = false;
private final long startTime = 50000;
private final long interval = 1000;
private Button start;
private TextView text,time_eplapsed;
private long timeElapsed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.startbutton);
start.setOnClickListener(this);
text = (TextView) findViewById(R.id.timer);
time_eplapsed = (TextView) findViewById(R.id.timeElapsed);
timer = new santimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime));
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (!timer_flag) {
timer.start();
timer_flag = true;
start.setText("Start");
} else {
timer.cancel();
timer_flag = false;
start.setText("Reset");
}
}
private class santimer extends CountDownTimer {
public santimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
text.setText("Time's up!");
time_eplapsed.setText("Time Elapsed: " + String.valueOf(startTime));
}
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
text.setText("Time remain:" + millisUntilFinished);
timeElapsed = startTime - millisUntilFinished;
time_eplapsed.setText("Time Elapsed: " + String.valueOf(timeElapsed));
}
}
}
Step 4 − No need to changemanifest.java
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run
icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −
Advertisements