Reputation: 23
Is there anyway to disable the past dates by using if statements? Or by using a try and catch.
I have done some research about this and I tried all the code people provided but I haven't had much luck.
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import java.sql.Time;
import java.text.DateFormat;
import java.util.Calendar;
public class MainActivity extends Activity {
private TextView OutputLogic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Spinner spSize = (Spinner)findViewById(R.id.spnSize);
final Spinner spTopping = (Spinner)findViewById(R.id.spnToppings);
final EditText etAddress = (EditText)findViewById(R.id.edAddress);
final Button btTime = (Button)findViewById(R.id.btnTime);
final Button btDay = (Button)findViewById(R.id.btnDay);
final Button btSubmit = (Button)findViewById(R.id.btnSubmit);
OutputLogic = (TextView)findViewById(R.id.tvOutput);
btDay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, d, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
final Calendar c = Calendar.getInstance();
DateFormat fmtDate = DateFormat.getDateInstance();
DatePickerDialog.OnDateSetListener d = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, monthOfYear);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
c.set(Calendar.MILLISECOND, c.getMinimum(Calendar.MILLISECOND));
OutputLogic.setText("The Time of your delivery is " + fmtDate.format(c.getTime()));
}
};
} // End of MainActivity
Upvotes: 1
Views: 53
Reputation: 922
You could try something like this:
DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, d, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
dialog.getDatePicker().setMinDate(System.currentTimeMillis());
dialog.show();
It simply would not show dates before today's date in DatePickerDialog itself. Hope that Helps.
Upvotes: 1