DatePicker with Current Date for First click and Selected date for Second Click:
Hi Guys! In this Tutorial we are going to see about DatePicker first open with current date and then second time click show selected date .Example:
public class MainActivity extends AppCompatActivity {
private int year;
private int month;
private int day;
private EditText signup_bday;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
signup_bday=(EditText)findViewById(R.id.edittext_signup_birthday);
signup_bday.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDatePickerDialog(signup_bday.getText().toString());
}
});
}
private void showDatePickerDialog(String date) {
if (date.contentEquals("")) {
Calendar mcurrentDate=Calendar.getInstance();
year=mcurrentDate.get(Calendar.YEAR);
month=mcurrentDate.get(Calendar.MONTH);
day=mcurrentDate.get(Calendar.DAY_OF_MONTH);
}else{
String[] split = date.split("-");
day = Integer.valueOf(split[0]);
int montx = Integer.valueOf(split[1]);
month = montx -1;
year = Integer.valueOf(split[2]);
}
DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int curyear, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
signup_bday.setText(new StringBuilder().append(dayOfMonth).append("-").append(monthOfYear + 1).append("-").append(curyear));
}
};
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
dateSetListener, year, month, day);
datePickerDialog.show();
}
}
No comments:
Post a Comment