Easy Tutorial step by step with free source code: count days from the moment you open the app until the settled date, in Android Studio version 3.1.3. For this tutorial, I counted the remaining days, hours, minutes and seconds until the Christmas 2018.

Create new project with Application name: Days until Christmas; Minimum SDK: API 21Android 5 (Lollipop).

Add 2 pictures, one for app background and one for countdown background, in res->drawable folder.

In manifest->AndroidManifest.xml write the code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.toyo.daysuntilchristmas">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

 

In app->res->values->strings.xml write the code:

<resources>
<string name="app_name">Days until Christmas</string>
    <string name="countdown">There are only</string>
    <string name="countdownleft">until Christmas!</string>
</resources>

 

In app->res->layout->activity_main.xml write the code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:background="@drawable/background"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/countdown"
android:textSize="25dp"
android:textColor="#ffeeee"
android:textStyle="bold"
android:layout_above="@+id/relativeLayout"
android:layout_centerHorizontal="true"
android:id="@+id/textView1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/relativeLayout">
<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/tvhappyevent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="50dp"
android:gravity="center"
android:singleLine="true"
android:text="Merry Christmas 2018!"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="gone" />
<LinearLayout
android:id="@+id/LinearLayout10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/counter"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerDay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#4a0000"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_TimerDay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Days"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/counter"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#4a0000"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_TimerHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Hours"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/counter"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerMinute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#4a0000"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_TimerMinute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Minutes"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/counter"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#4a0000"
android:textSize="35dp"
android:textStyle="bold" />
<TextView
android:id="@+id/txt_TimerSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Seconds"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/countdownleft"
android:textSize="35dp"
android:textColor="#ffeeee"
android:textStyle="bold"
android:layout_below="@+id/relativeLayout"
android:layout_centerHorizontal="true"
android:id="@+id/textView2" />
</RelativeLayout>

 

In app->java->MainActivity.java write the code:

package com.app.toyo.daysuntilchristmas;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView txtTimerDay, txtTimerHour, txtTimerMinute, txtTimerSecond;
private TextView tvEvent;
private Handler handler;
private Runnable runnable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtTimerDay = (TextView) findViewById(R.id.txtTimerDay);
txtTimerHour = (TextView) findViewById(R.id.txtTimerHour);
txtTimerMinute = (TextView) findViewById(R.id.txtTimerMinute);
txtTimerSecond = (TextView) findViewById(R.id.txtTimerSecond);
tvEvent = (TextView) findViewById(R.id.tvhappyevent);
countDownStart();
}
public void countDownStart() {
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
// Please here set your event date//YYYY-MM-DD
Date futureDate = dateFormat.parse("2018-12-25");
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;
txtTimerDay.setText("" + String.format("%02d", days));
txtTimerHour.setText("" + String.format("%02d", hours));
txtTimerMinute.setText(""
+ String.format("%02d", minutes));
txtTimerSecond.setText(""
+ String.format("%02d", seconds));
} else {
tvEvent.setVisibility(View.VISIBLE);
tvEvent.setText("The event started!");
textViewGone();
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 1 * 1000);
}
public void textViewGone() {
findViewById(R.id.LinearLayout10).setVisibility(View.GONE);
findViewById(R.id.LinearLayout11).setVisibility(View.GONE);
findViewById(R.id.LinearLayout12).setVisibility(View.GONE);
findViewById(R.id.LinearLayout13).setVisibility(View.GONE);
findViewById(R.id.textView1).setVisibility(View.GONE);
findViewById(R.id.textView2).setVisibility(View.GONE);
}
}

 

Run app in mobile device or Emulator (API 21 Android 5) and see how many days are left until Christmas 2018, in format landscape.

Follow video tutorial:

Download free Days Until Christmas 2018 application and install it on your mobile device to see how it works:
http://android-coffee.com/wp-content/uploads/projecs/DaysUntilChristmas2018.apk

 

Download this app for free and install it on your mobile device to see how it works:
http://android-coffee.com/wp-content/uploads/projecs/DaysUntilChristmas2018.apk

Download project with Source Code from here:
http://android-coffee.com/wp-content/uploads/projecs/DaysUntilChristmas2018.7z

Please subscribe here to our YouTube channel. Thank you!
Check next tutorials to learn all about Android.
For further questions leave a message.

Leave a Reply

Your email address will not be published. Required fields are marked *

Blue Captcha Image Refresh

*