Reputation: 1512
I searched a lot of datepicker and calendarview libraries to disable specific dates. Caldroid seems to have option to disable dates but it can only disable dates corresponding to current date. But I want to disable particular dates like 10, 18, 21, etc. there is no option for that. So it would be a great help if anyone can mention how to do it in caldroid or in any other library. What I did:
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void change(View v){
CaldroidFragment mCaldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
args.putInt( CaldroidFragment.START_DAY_OF_WEEK, CaldroidFragment.MONDAY );
mCaldroidFragment.setArguments( args );
ArrayList<String> disabledDates = new ArrayList<String>();
disabledDates.add("10");
disabledDates.add("20");
disabledDates.add("30");
ArrayList<Date> dd = new ArrayList<Date>();
for(int i=0; i<disabledDates.size(); i++){
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_WEEK, i);
dd.add(cal.getTime());
}
mCaldroidFragment.setDisableDates(dd);
getSupportFragmentManager().beginTransaction().replace( R.id.frame , mCaldroidFragment ).commit();
}
}
Upvotes: 1
Views: 979
Reputation: 2034
The following example works for me.
Add the following UI elements to your activity_main.xml
file:
<LinearLayout
android:id="@+id/calendar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/customize_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="customize"/>
</LinearLayout>
Adapt your MainActivity
as follows:
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import com.roomorama.caldroid.CaldroidFragment;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private CaldroidFragment caldroidFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
Calendar cal = Calendar.getInstance();
args.putInt( CaldroidFragment.START_DAY_OF_WEEK, CaldroidFragment.MONDAY );
caldroidFragment.setArguments(args);
// Attach to the activity
FragmentTransaction t = getSupportFragmentManager().beginTransaction();
t.replace(R.id.calendar1, caldroidFragment);
t.commit();
final Button customizeButton = (Button) findViewById(R.id.customize_button);
// Customize the calendar
customizeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Set disabled dates
try {
ArrayList<Date> disabledDates = new ArrayList<Date>();
disabledDates.add(getDate("2016-06-16"));
disabledDates.add(getDate("2016-06-20"));
disabledDates.add(getDate("2016-06-30"));
// Customize
caldroidFragment.setDisableDates(disabledDates);
// Refresh
caldroidFragment.refreshView();
} catch (ParseException e) {
e.printStackTrace();
}
}
});
}
private Date getDate(int year, int month, int day) {
Calendar calendar = Calendar.getInstance();
calendar.clear();
// month start at 1. Need to minus 1 to get javaMonth
calendar.set(year, month - 1, day);
return calendar.getTime();
}
private Date getDate(String strDate) throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse(strDate);
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.setTime(date);
return calendar.getTime();
}
}
When you click on the CUSTOMIZE button it will disable the following dates:
Upvotes: 1