Reputation: 1204
I want to disable the OK button in dialog fragment if user want to select the previous date and time from current time. This is the library https://github.com/jjobes/SlideDateTimePicker that I am using in my project by setting up minimum date I am able to prevent user to select previous date but didn't find a way for time .
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.github.jjobes.slidedatetimepicker.SlideDateTimeListener;
import com.github.jjobes.slidedatetimepicker.SlideDateTimePicker;
/**
* Sample test class for SlideDateTimePicker.
*
* @author jjobes
*
*/
@SuppressLint("SimpleDateFormat")
public class MainActivity extends FragmentActivity
{
private SimpleDateFormat mFormatter = new SimpleDateFormat("MMMM dd yyyy hh:mm aa");
private Button mButton;
private SlideDateTimeListener listener = new SlideDateTimeListener() {
@Override
public void onDateTimeSet(Date date)
{
Toast.makeText(MainActivity.this,
mFormatter.format(date), Toast.LENGTH_SHORT).show();
}
// Optional cancel listener
@Override
public void onDateTimeCancel()
{
Toast.makeText(MainActivity.this,
"Canceled", Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
new SlideDateTimePicker.Builder(getSupportFragmentManager())
.setListener(listener)
.setInitialDate(new Date())
.setMinDate(new Date())
//.setMaxDate(maxDate)
.setIs24HourTime(false)
//.setTheme(SlideDateTimePicker.HOLO_DARK)
//.setIndicatorColor(Color.parseColor("#990000"))
.build()
.show();
}
});
}
}
Is there any way to do this or suggest me if not so suggest me some link by which I put date and time picker on single dialog box . I checkout most solution but no one worked for me.
Upvotes: 2
Views: 1499
Reputation: 284
Simply use date picker which can't select previous date like this:
int mYear,mMonth,mDay;
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog=new DatePickerDialog(getContext(), R.style.datepicker,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
insertdate.setText(dayOfMonth + "-" + (month + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
datePickerDialog.show();
Upvotes: 0
Reputation: 112
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth)
{
dates = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;
txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
datePickerDialog.show();
Upvotes: 0
Reputation: 254
Ok, so I looked for this library, found it and compiled it to my project, and I saw that you have to take the project inside to yours and then add the following line to gradle:
dependencies {
compile project(':slideDateTimePicker')
}
Because of that, go to this library project files, edit the following file SlideDateTimeDialogFragment, inside it you will find the mOKButton code, edit it like the following:
NOTE: Don't forget to sync with gradle again afterwards.
Upvotes: 0
Reputation: 11457
Well you have to edit the library
1)Add the Library manually by adding as a package not using gradle
2)Go to this class SlideDateTimeDialogFragment.java
3) Find the declaration of onDateChanged and onTimeChanged
4) Now Add your validation in these methods
@Override
public void onDateChanged(int year, int month, int day)
{
mCalendar.set(year, month, day);
updateDateTab();
if(Your validations==true){
mOkButton.setEnabled(true);
}
else{
mOkButton.setEnabled(false);
}
}
And
@Override
public void onTimeChanged(int hour, int minute)
{
mCalendar.set(Calendar.HOUR_OF_DAY, hour);
mCalendar.set(Calendar.MINUTE, minute);
updateTimeTab();
if(Your validations==true){
mOkButton.setEnabled(true);
}
else{
mOkButton.setEnabled(false);
}
}
Upvotes: 1
Reputation: 2535
Following code will help to compare current date and selected date:
@Override
public void onDateTimeSet(Date date) {
if (date.before(new Date())) {
// past date
}else{
// future or current date
}
Toast.makeText(MainActivity.this,
mFormatter.format(date), Toast.LENGTH_SHORT).show();
}
Upvotes: 0