Current Date and set any specific date from Calender
In This project I am going to show you how date picker works. You can get the current date andalso you can pick any specific date as well.
In main.xml specifying two text view, one is for current date and another is for specific date that you select from calender. After clicking on button, one dialog will appear for calender, you can choose any specific date.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Today Date:"
android:textStyle="bold"/>
<TextView
android:id="@+id/tvCurrentDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Selected Date:" />
<TextView
android:id="@+id/tvselectedDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="@+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
CalenderViewActivity.java
public class CalenderViewActivity extends Activity {
private TextView tvDisplayTodayDate,tvModifiedDate;
private DatePicker dtPicker;
private Button btnChangeDate;
private int year;
private int month;
private int day;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
setCurrentDate();
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
tvModifiedDate = (TextView)findViewById(R.id.tvselectedDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog(101);
}
});
}
private DatePickerDialog.OnDateSetListener dpListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,int selectedMonth, int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Displaying selected date into textview
tvModifiedDate.setText(day+"-"+(month + 1)+"-"+year);
// Showing selected date into datepicker
//dtPicker.init(year, month, day, null);
}
};
protected Dialog onCreateDialog(int id) {
switch (id) {
case 101:
// set date picker as current date
return new DatePickerDialog(this, dpListener, year, month,day);
}
return null;
}
public void setCurrentDate() {
tvDisplayTodayDate = (TextView) findViewById(R.id.tvCurrentDate);
dtPicker = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
//Displaying today date into textview
tvDisplayTodayDate.setText(day+"-"+(month + 1)+"-"+year);
// Showing current date into datepicker
dtPicker.init(year, month, day, null);
}
}
No comments:
Post a Comment