I am using a text view, and i Want the countdown timer to display on this when I run the application on a set date.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.project.MainActivity"
android:id="@+id/drawer_layout">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:weightSum="1">
<include
layout="@layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/countdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Next Event:"
android:textStyle="bold"
android:textSize="25dp" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/navigation_view"
android:layout_gravity="start"
app:menu="@menu/drawer_menu"
app:headerLayout="@layout/navigation_drawer_header"
></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Above is my XML code for the application included is the text view named countdown.
private TextView txtCountdownEvent;
private Handler handler;
private Runnable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
txtCountdownEvent = (TextView) findViewById(R.id.countdown);
actionBarDrawerToggle = new
ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.draw_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
countDownStart();
}
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
public void countDownStart() {
handler = new Handler();
runnable = new Runnable() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
SimpleDateFormat dateFormat = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
}
// Please here set your event date//YYYY-MM-DD
Date futureDate = dateFormat.parse("2017-09-23");
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long diff = futureDate.getTime()
- currentDate.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);
long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);
long minutes = diff / (60 * 1000);
diff -= minutes * (60 * 1000);
long seconds = diff / 1000;
txtCountdownEvent.setText("" + String.format("%02d", days, hours, minutes, seconds));
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 1 * 1000);
Is it that I am unable to display days hours minutes and seconds in the one text view. the issue i have is when I run the appliction the timer does not display on the app. Any help is greatly appreciated.
The dateFormat
variable is null
You have not created an instance of SimpleDateFormat
for Devices Below Android Version N inside the try{ } catch () {}
Block
Sample code to correct this:
SimpleDateFormat dateFormat = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
} else {
//Set the optional Date format here for Devices Running ANDROID VERSION BELOW N
dateFormat = new SimpleDateFormat("yyyy-MM-dd");
}