Reputation: 465
I would love to make a popup datepicker ,while click the EditText ,And the date can be set to different languages according to the device 's language .I will need it use Kotlin. More or less like as below : Could anyone show me an example please ? Thank you so much in advance !
I have tried this link ,but apppears two problems:
https://developer.android.com/guide/topics/ui/controls/pickers
DatePickerActivity.kt
class DatePickerActivity : AppCompatActivity() {
val myCalendar: Calendar = Calendar.getInstance()
lateinit var edt : EditText
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_date_picker)
val day :Int = myCalendar.get(Calendar.DAY_OF_MONTH)
val month:Int = myCalendar.get(Calendar.MONTH)
val year :Int = myCalendar.get(Calendar.YEAR)
edt = findViewById(R.id.edt_birthday)
edt.setOnClickListener {
showDatePickerDialog(it)
edt.setText("" + day+ " " + month+1 + ", " + year)
}
}
fun showDatePickerDialog(v: View){
val newFragment = DatePickerFragment()
newFragment.show(supportFragmentManager,"datePicker")
}
}
DatePickerFragment.kt
import android.app.DatePickerDialog
import android.app.Dialog
import android.os.Bundle
import android.widget.DatePicker
import androidx.fragment.app.DialogFragment
import java.util.*
class DatePickerFragment : DialogFragment(), DatePickerDialog.OnDateSetListener {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
// Use the current date as the default date in the picker
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
// Create a new instance of DatePickerDialog and return it
return DatePickerDialog(requireActivity(), this, year, month, day)
}
override fun onDateSet(view: DatePicker, year: Int, month: Int, day: Int) {
}
}
Upvotes: 0
Views: 829
Reputation: 93659
The default DatePicker looks like a calendar. You can change the behavior back to the old spinner style by adding this extra style in themes.xml
.
<style name="MyDatePickerStyle" parent="android:Widget.Material.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
and then inside your style for the app's theme, add this line:
<item name="android:datePickerStyle">@style/MyDatePickerStyle</item>
Note that the calendar style is the default because it has been found to be preferred by most users.
Upvotes: 3