1

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
Suraj Makhija
  • 1,376
  • 8
  • 16
Drizzy
  • 23
  • 2
  • Where is the code you tried? to disable past dates – Fady Saad May 05 '17 at 04:26
  • Why are you having code after `onCreate()`, outside all methods? – Pang May 05 '17 at 04:37
  • you can refer this : http://stackoverflow.com/questions/13661788/how-to-set-minimum-datepicker-date-to-current-date – sapan ravani May 05 '17 at 04:48
  • Pang, I've always done it this way in my class. Done it with all my apps I've created. The book im using done it this way and im doing as the book taught me how to do it. – Drizzy May 05 '17 at 20:42

1 Answers1

1

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.

Ankur
  • 892
  • 6
  • 11
  • Ill try it and get back to you. – Drizzy May 05 '17 at 20:41
  • It does work, but when i select the past dates, it still shows up in my results, i wanna stop the user to selecting those dates and having the information go through with it. – Drizzy May 05 '17 at 20:49
  • @Drizzy, I don't really get you. If datepicker does not show past dates, then how is your user able to select past dates in the first place. – Ankur May 06 '17 at 11:24