In android we can use DatePicker to select a date. When we want that user should select a date we use DatePicker.
When user select some date and click on OK button we will show the selected date in textView.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonSelectDate"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:textSize="23dp"
android:text=" OK " />
<TextView
android:id="@+id/textViewDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Date and Time Selected: "
android:textSize="17dp" />
</LinearLayout>
{
DatePicker datePicker;
TextView textView;
Button btnDate;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datePicker=(DatePicker)findViewById(R.id.datePicker1);
textView=(TextView)findViewById(R.id.textViewDate);
btnDate=(Button)findViewById(R.id.buttonSelectDate);
btnDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// get the selected day, month and year
int day=datePicker.getDayOfMonth();
int month=datePicker.getMonth();
int year=datePicker.getYear();
// show the selected date in textView
textView.setText("Date Selected "+day+":"+month+":"+year);
}
});
}
}
main.xml
In layout we have a datepicker, a button, and a textViewWhen user select some date and click on OK button we will show the selected date in textView.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonSelectDate"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:textSize="23dp"
android:text=" OK " />
<TextView
android:id="@+id/textViewDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:text="Date and Time Selected: "
android:textSize="17dp" />
</LinearLayout>
DatePickerActivity.java
public class DatePickerActivity extends Activity{
DatePicker datePicker;
TextView textView;
Button btnDate;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
datePicker=(DatePicker)findViewById(R.id.datePicker1);
textView=(TextView)findViewById(R.id.textViewDate);
btnDate=(Button)findViewById(R.id.buttonSelectDate);
btnDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// get the selected day, month and year
int day=datePicker.getDayOfMonth();
int month=datePicker.getMonth();
int year=datePicker.getYear();
// show the selected date in textView
textView.setText("Date Selected "+day+":"+month+":"+year);
}
});
}
}
No comments:
Post a Comment