In this tutorial I want to explain about creating a DatePickerDialog like,
In this I am having One Button and One EditText. On this example first it will load the current date to the EditText, While Tap on the button it will display the DatePickerDialog after tap on Done button DatePickerDialog it will update in the EditText.
activity_main.xml
This is a Layout of main page. This layout contain One Button and one EditText.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/BtnSelectDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date" />
<EditText
android:id="@+id/EdtTxtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:focusableInTouchMode="false"
android:inputType="text" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
Button BtnSelectImage;
private EditText EdtTxtDate;
private static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=MainActivity.this;
EdtTxtDate = (EditText) findViewById(R.id.EdtTxtDate);
String Date = getCurrentDate();
EdtTxtDate.setText(Date);
BtnSelectDate = (Button) findViewById(R.id.BtnSelectDate);
BtnSelectDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
int monthOfYear, dayOfMonth, year;
String DateString = EdtTxtDate.getText().toString().trim();
if(DateString.equalsIgnoreCase("")){
DateString = getCurrentDate();
}
dayOfMonth = Integer.parseInt(DateString.substring(0,DateString.indexOf("-")));
monthOfYear = Integer.parseInt(DateString.substring(DateString.indexOf("-")+1,DateString.lastIndexOf("-")))-1;
year = Integer.parseInt(DateString.substring(DateString.lastIndexOf("-")+1));
DatePickerDialog datePickerDialog;
datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
monthOfYear = monthOfYear+1;
String Month="", Day ="";
if(monthOfYear<10)
Month = "0"+ monthOfYear;
else
Month = monthOfYear+"";
if(dayOfMonth<10)
Day = "0"+ dayOfMonth;
else
Day = dayOfMonth+"";
String AlertDate = Day+"-"+Month+"-"+year;
EdtTxtDate.setText(AlertDate);
}
}, year, monthOfYear, dayOfMonth);
datePickerDialog.setTitle("Select Date");
datePickerDialog.show();
}
});
}
//Function to get current date
public static String getCurrentDate() {
String DATE_FORMAT_NOW = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(new Date());
}
}
In this I am having One Button and One EditText. On this example first it will load the current date to the EditText, While Tap on the button it will display the DatePickerDialog after tap on Done button DatePickerDialog it will update in the EditText.
activity_main.xml
This is a Layout of main page. This layout contain One Button and one EditText.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top|center"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/BtnSelectDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Date" />
<EditText
android:id="@+id/EdtTxtDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:focusableInTouchMode="false"
android:inputType="text" />
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
Button BtnSelectImage;
private EditText EdtTxtDate;
private static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=MainActivity.this;
EdtTxtDate = (EditText) findViewById(R.id.EdtTxtDate);
String Date = getCurrentDate();
EdtTxtDate.setText(Date);
BtnSelectDate = (Button) findViewById(R.id.BtnSelectDate);
BtnSelectDate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
int monthOfYear, dayOfMonth, year;
String DateString = EdtTxtDate.getText().toString().trim();
if(DateString.equalsIgnoreCase("")){
DateString = getCurrentDate();
}
dayOfMonth = Integer.parseInt(DateString.substring(0,DateString.indexOf("-")));
monthOfYear = Integer.parseInt(DateString.substring(DateString.indexOf("-")+1,DateString.lastIndexOf("-")))-1;
year = Integer.parseInt(DateString.substring(DateString.lastIndexOf("-")+1));
DatePickerDialog datePickerDialog;
datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
monthOfYear = monthOfYear+1;
String Month="", Day ="";
if(monthOfYear<10)
Month = "0"+ monthOfYear;
else
Month = monthOfYear+"";
if(dayOfMonth<10)
Day = "0"+ dayOfMonth;
else
Day = dayOfMonth+"";
String AlertDate = Day+"-"+Month+"-"+year;
EdtTxtDate.setText(AlertDate);
}
}, year, monthOfYear, dayOfMonth);
datePickerDialog.setTitle("Select Date");
datePickerDialog.show();
}
});
}
//Function to get current date
public static String getCurrentDate() {
String DATE_FORMAT_NOW = "dd-MM-yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(new Date());
}
}
No comments:
Post a Comment