날짜를 선택에 활용되는 DatePickerDialog 예제를 기록하려고 합니다.
해당 예제는 위 그림과 같이 시작 날짜 ~ 끝 날짜를 설정하는 내용을 담고 있습니다.
1. DatePickerDialog (kotlin)
private lateinit var binding: ActivityMainBinding
private var startCalendar = Calendar.getInstance()
private var endCalendar = Calendar.getInstance()
private val dateFormat = SimpleDateFormat("yyyy-MM-dd")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
startCalendar.add(Calendar.MONTH, -1) // startCalendar 한달 전으로 설정
binding.btnStartDate.text = dateFormat.format(startCalendar.time)
binding.btnEndDate.text = dateFormat.format(endCalendar.time)
binding.btnStartDate.setOnClickListener {
// 날짜 선택 리스너 정의
val onDateSetListener = DatePickerDialog.OnDateSetListener { _, year, month, day ->
startCalendar.set(Calendar.YEAR, year)
startCalendar.set(Calendar.MONTH, month)
startCalendar.set(Calendar.DAY_OF_MONTH, day)
binding.btnStartDate.text = dateFormat.format(startCalendar.time)
}
// DatePickerDialog 생성, 날짜 선택 리스너 등록 및 날짜 설정
val datePickerDialog = DatePickerDialog(
this,
onDateSetListener,
startCalendar.get(Calendar.YEAR),
startCalendar.get(Calendar.MONTH),
startCalendar.get(Calendar.DAY_OF_MONTH)
)
// DatePickerDialog Show
datePickerDialog.show()
}
binding.btnEndDate.setOnClickListener {
// 날짜 선택 리스너 정의
val onDateSetListener = DatePickerDialog.OnDateSetListener { _, year, month, day ->
endCalendar.set(Calendar.YEAR, year)
endCalendar.set(Calendar.MONTH, month)
endCalendar.set(Calendar.DAY_OF_MONTH, day)
binding.btnEndDate.text = dateFormat.format(endCalendar.time)
}
// DatePickerDialog 생성, 날짜 선택 리스너 등록 및 날짜 설정
val datePickerDialog = DatePickerDialog(
this,
onDateSetListener,
endCalendar.get(Calendar.YEAR),
endCalendar.get(Calendar.MONTH),
endCalendar.get(Calendar.DAY_OF_MONTH)
)
// DatePickerDialog Show
datePickerDialog.show()
}
}
}
2. DatePickerDialog (java)
private Calendar startCalendar = Calendar.getInstance();
private Calendar endCalendar = Calendar.getInstance();
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStartDate = findViewById(R.id.btnStartDate);
Button btnEndDate = findViewById(R.id.btnStartDate);
startCalendar.add(Calendar.MONTH, -1); // startCalendar 한달 전으로 설정
btnStartDate.setText(dateFormat.format(startCalendar.getTime()));
btnEndDate.setText(dateFormat.format(endCalendar.getTime()));
btnStartDate.setOnClickListener(view -> {
// 날짜 선택 리스너 정의
DatePickerDialog.OnDateSetListener onDateSetListener = (v, year, month, day) -> {
startCalendar.set(Calendar.YEAR, year);
startCalendar.set(Calendar.MONTH, month);
startCalendar.set(Calendar.DAY_OF_MONTH, day);
btnStartDate.setText(dateFormat.format(startCalendar.getTime()));
};
// DatePickerDialog 생성, 날짜 선택 리스너 등록 및 날짜 설정
DatePickerDialog datePickerDialog = new DatePickerDialog(
this,
onDateSetListener,
startCalendar.get(Calendar.YEAR),
startCalendar.get(Calendar.MONTH),
startCalendar.get(Calendar.DAY_OF_MONTH)
);
// DatePickerDialog Show
datePickerDialog.show();
});
btnEndDate.setOnClickListener(view -> {
// 날짜 선택 리스너 정의
DatePickerDialog.OnDateSetListener onDateSetListener = (v, year, month, day) -> {
endCalendar.set(Calendar.YEAR, year);
endCalendar.set(Calendar.MONTH, month);
endCalendar.set(Calendar.DAY_OF_MONTH, day);
btnEndDate.setText(dateFormat.format(endCalendar.getTime()));
};
// DatePickerDialog 생성, 날짜 선택 리스너 등록 및 날짜 설정
DatePickerDialog datePickerDialog = new DatePickerDialog(
this,
onDateSetListener,
endCalendar.get(Calendar.YEAR),
endCalendar.get(Calendar.MONTH),
endCalendar.get(Calendar.DAY_OF_MONTH)
);
// DatePickerDialog Show
datePickerDialog.show();
});
}
}
3. layout 구성
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btnStartDate"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="17dp" />
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="~"
android:textSize="17dp" />
<Button
android:id="@+id/btnEndDate"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="17dp" />
</LinearLayout>
</LinearLayout>
</layout>