Unable to pick future date in my calendar, and also not able to get date as 2 digit

97 Views Asked by At

This is my kotlin file

class MainActivity : AppCompatActivity() {
var datePicker: DatePickerDialog? = null
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val c = Calendar.getInstance()
    val year = c.get(Calendar.YEAR)
    val month = c.get(Calendar.MONTH)
    val day = c.get(Calendar.DAY_OF_MONTH)

    calen_box.setOnClickListener {
        datePicker =  DatePickerDialog(this, DatePickerDialog.OnDateSetListener { view: DatePicker?, year: Int, month: Int, dayOfMonth: Int ->
            daytext.setText(String.format("%2d", day))
            monthtext.setText(String.format("%2d", (month+1)))
            yeartext.setText(String.format("%d", day))
        }, year, month, day)
        datePicker!!.datePicker.maxDate = c.timeInMillis
        datePicker!!.show()
    }

}

my xml file

<LinearLayout
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"
android:orientation="vertical"
android:background="@color/colorAccent"
tools:context=".MainActivity">
   <LinearLayout
       android:id="@+id/calen_box"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal"
       android:background="@drawable/rect">

       <TextView
           android:id="@+id/daytext"
           android:layout_width="90dp"
           android:layout_height="match_parent"
         />
       <ImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/line"
           android:layout_gravity="center"/>
       <TextView
           android:id="@+id/monthtext"
           android:layout_width="130dp"
           android:layout_height="match_parent"
           />
       <ImageView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/line"
           android:layout_gravity="center"/>
       <TextView
           android:id="@+id/yeartext"
           android:layout_width="130dp"
           android:layout_height="match_parent"
           />
   </LinearLayout>

When I click on datepicker it only select today's date, can't able to set future date, and also it gets date in single digit. I need date as "YYYY-MM-DD" format Can't able to find out what wrong I did in that code, can you please help me to solve my problem

0

There are 0 best solutions below