<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
```
In your Java or Kotlin code, you can interact with the `DatePicker`:
```java
public class YourActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
DatePicker datePicker = findViewById(R.id.datePicker);
// You can set an initial date if needed
// datePicker.init(year, month, dayOfMonth, null);
datePicker.setOnDateChangedListener(new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// Handle the selected date
String selectedDate = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
// Do something with the selected date
}
});
}
}
```
In this example, the `DatePicker.OnDateChangedListener` is used to listen for changes in the selected date. The `onDateChanged` method is called when the user selects a new date. You can customize the code inside this method to perform actions based on the selected date.
Remember to replace `"your_layout"` with the actual name of your layout XML file.
Comments
Post a Comment