aenugula karthik
aenugula karthik

Reputation: 359

I cant be able to set datepicker starts from tomorrows date(Material Design Datepicker)

Iam using Material Design Datepicker i can be able to set from tomorrows date but todays date is also showing in calendar i set for 29/11/2016 but it also showing 28/11/2016 How to disable that so that user cant select it Iam using Material Design DatePicker https://github.com/wdullaer/MaterialDateTimePicker

    GregorianCalendar g1=new GregorianCalendar();
    g1.add(Calendar.DATE, 1);
    GregorianCalendar gc = new GregorianCalendar();
    gc.add(Calendar.DAY_OF_MONTH, 30);
    dateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DatePickerDialog datePickerDialog ;
            datePickerDialog = DatePickerDialog.newInstance(LoginSuccess.this, g1.get(Calendar.YEAR),
                    g1.get(Calendar.MONTH),
                    g1.get(Calendar.DAY_OF_MONTH));

            datePickerDialog.setTitle("Select Date");
            //datePickerDialog.setMinDate(g);
            GregorianCalendar c2=new GregorianCalendar();
            c2.setTimeInMillis(g1.getTimeInMillis()+(27*7*24*60*60*1000));
            //c2.add(Calendar.DAY_OF_MONTH, 30);
            List<Calendar> dayslist= new LinkedList<Calendar>();
            Calendar[] daysArray;
            Calendar cAux = Calendar.getInstance();
            while ( cAux.getTimeInMillis() <= gc.getTimeInMillis())
            {
                if (cAux.get(Calendar.DAY_OF_WEEK)!=1)
                {
                    Calendar c = Calendar.getInstance();
                    c.setTimeInMillis(cAux.getTimeInMillis());
                    dayslist.add(c);
                }
                cAux.setTimeInMillis(cAux.getTimeInMillis()+(24*60*60*1000));
            }
            daysArray = new Calendar[dayslist.size()];
            for (int i = 0; i<daysArray.length;i++)
            {
                daysArray[i]=dayslist.get(i);
            }
            //g1.setTimeInMillis(g.getTimeInMillis()+(7*24*60*60*1000));
            datePickerDialog.setMinDate(g1);
            datePickerDialog.setMaxDate(c2);
            datePickerDialog.show(getFragmentManager(), "DatePickerDialog");
            datePickerDialog.setSelectableDays(daysArray);

        }

Upvotes: 1

Views: 545

Answers (5)

Paul Hobday
Paul Hobday

Reputation: 1

From what I recall from the documentation for this library, I believe that if you set the selectable days for MaterialDateTimePicker then it overrides anything you have done set for min and max date. So if you pass an array of dates it will need to include your min and max dates, plus everything you want to see in between.

Upvotes: 0

G.Karthik
G.Karthik

Reputation: 76

Hey i have done sample regarding the issue and able to get the date picker as you expect once check it and let me know Java:

package yourpackage;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog; 
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
private DatePicker datePicker;
private Calendar calendar;
private TextView dateView;
private int year, month, day;

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

    dateView = (TextView) findViewById(R.id.textView3);
    calendar = Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);

    month = calendar.get(Calendar.MONTH);
    day = calendar.get(Calendar.DAY_OF_MONTH);
    showDate(year, month + 1, day);
}

@SuppressWarnings("deprecation")
public void setDate(View view) {
    showDialog(1);
    Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
            .show();
}

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    if (id == 1) {
        return new DatePickerDialog(this, myDateListener, year, month,
                day + 1);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        // arg1 = year
        // arg2 = month
        // arg3 = day
        showDate(arg1, arg2 + 1, arg3);
    }
};

private void showDate(int year, int month, int day) {
    dateView.setText(new StringBuilder().append(day).append("/")
            .append(month).append("/").append(year));
}

}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
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"
tools:context=".MainActivity" >

<Button
  android:id="@+id/button1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="70dp"
  android:onClick="setDate"
  android:text="@string/date_button_set" />

<TextView
  android:id="@+id/textView1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="24dp"
  android:text="@string/date_label_set"
  android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
  android:id="@+id/textView2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/button1"
  android:layout_marginTop="66dp"
  android:layout_toLeftOf="@+id/button1"
  android:text="@string/date_view_set"
  android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
  android:id="@+id/textView3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignRight="@+id/button1"
  android:layout_below="@+id/textView2"
  android:layout_marginTop="72dp"
  android:text="@string/date_selected"
  android:textAppearance="?android:attr/textAppearanceMedium" />

 </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>

Values

<resources>
<string name="app_name">Androidr</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="text_label_1">Rate</string>
<string name="button_label">Submit</string>
<string name="text_label_2">Result: </string>
<string name="action_settings">Settings</string>
<string name="date_label_set">Press the button to set the date</string>
<string name="date_button_set">Set Date</string>
<string name="date_view_set">The Date is: </string>
<string name="date_selected"></string>
</resources>

Upvotes: 1

Devraj
Devraj

Reputation: 1531

You should do this,

Calendar cal = Calendar.getInstance();
            cal.set(Calendar.DAY_OF_MONTH, day + 1);
            cal.set(Calendar.MONTH, month);
            cal.set(Calendar.YEAR, year);

 datePickerDialog.getDatePicker().setMaxDate(maxDate.getTimeInMillis());

this will restrict you to select current date and let you select the date starts from tomorrow.

Upvotes: 0

android_griezmann
android_griezmann

Reputation: 3887

Try this out!

dateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Calendar now = Calendar.getInstance();
            now.add(Calendar.DAY_OF_YEAR, 1);
            DatePickerDialog dpd = DatePickerDialog.newInstance(
                    MainActivity.this,
                    now.get(Calendar.YEAR),
                    now.get(Calendar.MONTH),
                    now.get(Calendar.DAY_OF_MONTH)
            );
            dpd.setMinDate(now);
            dpd.setThemeDark(modeDarkDate.isChecked());
            dpd.vibrate(vibrateDate.isChecked());
            dpd.dismissOnPause(dismissDate.isChecked());
            dpd.showYearPickerFirst(showYearFirst.isChecked());
            if (modeCustomAccentDate.isChecked()) {
                dpd.setAccentColor(Color.parseColor("#9C27B0"));
            }
            if(titleDate.isChecked()) {
                dpd.setTitle("DatePicker Title");
            }
            dpd.show(getFragmentManager(), "Datepickerdialog");
        }
    });
}

Upvotes: 1

naveed abbas
naveed abbas

Reputation: 63

 g1.add(Calendar.DATE, +1);

check it like this

Upvotes: 0

Related Questions