Subscribe Us

Age calculator project in Android and Kotlin.

You can create your own xml file to design you application,
so I am not giving here the xml file. But you can access the
whole project from my Github account. link is given below.


3:  import android.app.DatePickerDialog  
4:  import android.os.Bundle  
5:  import android.util.Log  
6:  import androidx.appcompat.app.AppCompatActivity  
7:  import kotlinx.android.synthetic.main.activity_main.*  
8:  import java.text.SimpleDateFormat  
9:  import java.time.LocalDate  
10:  import java.util.*  
11:    
12:  class MainActivity : AppCompatActivity() {  
13:    lateinit var selected_date: Date  
14:    lateinit var yourAge: String  
15:    lateinit var date_formater: SimpleDateFormat  
16:    override fun onCreate(savedInstanceState: Bundle?) {  
17:      super.onCreate(savedInstanceState)  
18:      setContentView(R.layout.activity_main)  
19:      date_formater = SimpleDateFormat("dd/MM/yyyy") // It will change the date into a string buffer in this pattern  
20:      val today_date = date_formater.format(Date()) // format function took a date as a argument and Date() is function which returns today's date  
21:      showing_today_date.setText(today_date)  
22:      click_to_show_cal.setOnClickListener{  
23:        Opendatepicker()  
24:      }  
25:      calculateBtn.setOnClickListener {  
26:        showing_your_age.setText(yourAge) // Showing the age of person after clicking the calculate button  
27:      }  
28:    }  
29:    @Suppress("DEPRECATION")  
30:    fun Opendatepicker() {  
31:      val cal = Calendar.getInstance() // Creating a calendar instance to open a calendar form your phone  
32:      val date = cal.time  
33:      val year = date.year  
34:      val month = date.month  
35:      val day = date.day  
36:      DatePickerDialog(this, DatePickerDialog.OnDateSetListener { View, selectYear, selectMonth, selectDay ->  
37:        selected_date = Date(selectYear - 1900, selectMonth, selectDay)  
38:        val show_date = date_formater.format(selected_date)  
39:        showing_selected_date.setText(show_date)  
40:        this.yourAge = claculate(selectYear, selectMonth, selectDay)  
41:        // calculate is a function which return a string which is shown as a string, top of the your age. created below  
42:      }, year, month, day).show() // In this line the year, month and day are the same as line 33, 34 and 35  
43:    }  
44:    fun claculate(selected_year: Int, selected_month: Int, selected_day: Int): String {  
45:      val date_today = LocalDate.now()  
46:      val todayDay = date_today.dayOfMonth  
47:      val todayMonth = date_today.monthValue  
48:      val todayYear = date_today.year  
49:      var age_year:Int  
50:      var age_month:Int  
51:      var age_day:Int  
52:    
53:      //Logic to find age and after calculating returning the string  
54:      if (todayYear > selected_year) {  
55:        if ((todayMonth >= selected_month) && (todayDay >= selected_day)) {  
56:          age_year = todayYear - selected_year  
57:          age_month = todayMonth - (selected_month+1)  
58:          age_day = todayDay - selected_day  
59:        } else if (todayMonth < selected_month && todayDay < selected_day) {  
60:          age_year = (todayYear-1) - selected_year  
61:          age_month = ((todayMonth + 12)-1) - (selected_month+1)  
62:          age_day = (todayDay + 30) - selected_day  
63:        } else if (todayMonth < selected_month) {  
64:          age_year = (todayYear-1) - selected_year  
65:          age_month = (todayMonth + 12) - (selected_month+1)  
66:          age_day = todayDay - selected_day  
67:        } else if (todayDay < selected_day) {  
68:          age_year = todayYear - selected_year  
69:          age_month = (todayMonth -1) - (selected_month+1)  
70:          age_day = (todayDay + 30) - selected_day  
71:        } else {  
72:          return "You Entered Wrong Date"  
73:        }  
74:        return "$age_year Years, $age_month Months, $age_day Days"  
75:      } else {  
76:        return "You have not born yet"  
77:      }  
78:    }  
79:  }  
80:    

Github Link is Here.
Apk file is uploaded on github also.

Age calculator project in Android and Kotlin. Age calculator project in Android and Kotlin. Reviewed by Coding Arc on January 13, 2021 Rating: 5

No comments:

Powered by Blogger.