Guide to designing and using calendars in Android apps
5 minute(s) read
Published on: Aug 18, 2021
Updated on: Jan 27, 2022
As you know, phones have become the most used objects that users use, so mobile phones should have more features. For example, in addition to other features available in phones for the everyday use of users, the Calendar is also one of the most used options that phones should have. In this part of the tutorial, we want to talk about how to create a calendar on Android apps, return to the previous page in the application, and the list view.
To be able to reject the Calendar on Android apps (in Slovak: Aplikácie pre Android ) and use them, we must create the Calendar inside the layer and transfer it to Java.
1- We create a new project and choose the desired name for it. In this article, we have selected the project name Calendar-Gsm.
2- From the Date & Time folder in the Palette section of activity_main.xml, we add a CalendarView to the project.
3- The codes that we need to add for the Calendar are as follows:
4- Then, we make the necessary changes in the MainActivity.java section.
5- We named CalendarView and introduced it to the Java code of the program.
6-Add an onDateChangeListener event to CalendarView.
7- We create a function called onSelectedDayChange inside the onDateChangeListener event so that it can show the day, Month, and year using Toast.
8- Then, we perform the steps listed below in the onCreate function.
9- Introduce and name CalendarView in MainActivity.java as follows:
10- Then create the setOnDateChangeListener event.
11- Then, we use the onSelectedDayChange function to show the day, Month, Year and introduce it.
12- The complete codes that should be in the MainActivity section are as follows:
13- We run the program.
14- We will see that the output of the program is such that by tapping any date, its day, year, and Month are displayed to users.
Back button in apps
To go back to the previous pages, there must be a button, and a backward definition must be defined for it so that we can finally go back. In this section, we also want to teach you how to return to the previous pages using the back buttons.
1- We create a new project.
2- Select the desired name for it, such as BackButton-Gsm.
3- Apply changes to the MainActivity.java file. In this case, in the AlertDialog section, we enter codes so that by clicking those buttons, the user will be transferred to the previous page.
4- There is a function called onBackPressed, which is in the Java part of the program, and when we click on the return button, the codes inside that function start to run.
5- Create and write the onBackPressed function.
6- Then, we need to define backButtonEvent.
7- It is better to define AlertDialog codes inside this function first.
8- Select the name of its variable ALERT.
9- Then, we must specify the title and text of the message.
It should be noted that AlertDialog has two modes, positive mode and negative mode. In other words, it can be said that it has a positive state for the yes button and a negative state for the no button.
10- The positive modes of AlertDialog are as follows:
11- The positive modes of AlertDialog are as follows:
12- The codes of the backButtonEvent section are as follows:
13- The complete codes that should be included in the MainActivity section are as follows:
14- We run the program.
15- After running the program, we will see that by clicking the return button, a window will be shown that the window will ask us if we want to leave the program or not.
If we select the Yes button, it will be removed from the program, and if we select the No button, it will not be removed from the program.
How to create a view list and set it
1- Create a new project and choose its name ListView-Gsm.
2- In the Containers section, select the ListView tool from the Palette section and add it to your application layer.
3- The codes that should be in the ListView section are as follows:
4- Go to the Java section of the program and write the related code there.
5- Apply the changes listed below in the MainActivity.java section.
6- First, we create a variable of type String and set it.
7- In this section, we have set it as a Month. Like the following:
8- Then, we add an ArrayAdapter. By doing this, we cause the data that is taken to be listed.
9- We write these codes inside the onCreate () function.
10- Then, we introduce the view list to the program using the generator.
11- We also write the codes inserted below inside the onCreate () function.
12- The following code must also be inserted inside the onCreate ().
13- The complete codes and contents that should be in the MainActivity section are as follows:
In this line of code, we have written an error in R.layout.activity_listview.
What can be done to resolve this error?
14- Go to Res section and then Layout.
15- Right click on Layout.
16- Select the Layout resource file on the new section and click.
17- Then in the window that opens, select the name activity_listview for Layout.
18- Then we will see that Layout has been added to the program.
19- Then we must enter the code below in the activity_listview.xml section.
20- Run the program to see its output.
21- In this case, we will see that all data and information are displayed as a list in the output.About our Android and iOS app development services