This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Dhruv Joshi
To create a clock app in Android Studio, you can follow these steps:
- Open Android Studio and create a new project with an Empty Activity.
- In the main activity layout file (activity_main.xml), add a TextView element to display the current time.
- In the MainActivity.java file, you can use the Calendar class to get the current time and display it in the TextView. You can do this in the onCreate() method, like this:
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
String currentTime = simpleDateFormat.format(calendar.getTime());
TextView textView = findViewById(R.id.text_view);
textView.setText(currentTime);
- To update the time in the TextView every second, you can use a Handler and a Runnable to run a piece of code every second.
final TextView textView = findViewById(R.id.text_view);
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
@Override
public void run() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
String currentTime = simpleDateFormat.format(calendar.getTime());
textView.setText(currentTime);
handler.postDelayed(this, 1000);
}
};
handler.post(runnable);
- To display the date in addition to the time, you can add another TextView element and update it in the same way as the time TextView.
- To test your app, click the Run button in the toolbar. Choose a device or emulator to run the app on and click "OK."
I hope this helps! Let me know if you have any other questions. Reach me if you have ideas and want to develop apps.
This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Dhruv Joshi
Dhruv Joshi | Sciencx (2022-12-26T05:06:54+00:00) How to make a clock Android app in Android Studio?. Retrieved from https://www.scien.cx/2022/12/26/how-to-make-a-clock-android-app-in-android-studio/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.