1

Below is my code that enables a datedialog to pop up once a edittext is clicked. My main problem is restricting the user from selecting past dates as input.

MainActivity.java

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Spinner spinner_from = (Spinner) findViewById(R.id.spinFrom);
        final Spinner spinner_to = (Spinner) findViewById(R.id.spinTo);
        Button Searchbtn = (Button) findViewById(R.id.btn_search);

        Searchbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String location = spinner_from.getSelectedItem().toString();
                String destination = spinner_to.getSelectedItem().toString();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //adds icons to the action bar
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        //to handle actions performed by icons on action bar
        switch (item.getItemId()){

            case R.id.admin_button:
                startActivity(new Intent(this, AdminLogin.class));
                return true;

            case R.id.action_about:
                startActivity(new Intent(this, AboutBookBus.class));
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    //to handle the date dialog and set it to the edittext
    public void onStart(){
        super.onStart();
        EditText travelDate = (EditText)findViewById(R.id.travelDate);
        travelDate.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                DateDialog dialog = new DateDialog(view);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                dialog.show(ft, "DatePicker");
            }


        });
    }
    }

DateDialog.java

public class DateDialog extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    EditText travelDate;
    String Current_date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());

    public DateDialog(View view) {
        travelDate = (EditText) view;
    }

    //dialog context to set the date
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        String date = year + "-" + (month + 1) + "-" + day;
        travelDate.setText(date);


    }
}

Edittext section in activity_main.xml

<EditText
        android:layout_width="350dp"
        android:layout_height="42dp"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/travelDate"
        tools:ignore="HardcodedText,TextFields"
        android:hint="Touch to add travel date"
        app:layout_constraintLeft_toLeftOf="@+id/textView"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintRight_toRightOf="@+id/textView"
        app:layout_constraintHorizontal_bias="0.75" />

Your feedback and help will be greatly appreciated.

Godwin
  • 37
  • 8
  • Use setMinDate and setMaxDate function to limit the Calendar View. And please google and browse along related SO question before posting. – nnn Feb 16 '17 at 11:59
  • I apologise for the redundancy but I tried all other related thread but to no avail. – Godwin Feb 17 '17 at 13:19

3 Answers3

2

Use setMinDate method of DatePicker class.

datePicker.setMinDate(System.currentTimeMillis() - 1000);

Ganesh AB
  • 4,652
  • 2
  • 18
  • 29
0

First select current date

Calendar c = Calendar.getInstance();
Date currentDate= c.getTime();

Now compare the date with selected date

if (selectedDate.after(currentDate)) {
    // Do something here
} else {
    // Print here "Please select date after current date"
}
Ziem
  • 6,579
  • 8
  • 53
  • 86
0

Used this

Calendar calendar=Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH,-1);
    dialog.getDatePicker().setMaxDate(calendar.getTime().getTime());

This is best way to restrict yesterdays date.

Rahul Giradkar
  • 1,818
  • 1
  • 17
  • 28