Question

Unfortunately I have unable to found any help regarding this.

Actually I want to fetch all available calendars(may be calendar preference) list from mobile for example Google calendar, yahoo calendar.

For better explanation I captured some images from Smooth Calendar application which is in below image after config button click from the widget.

enter image description here ==> enter image description here

Here the Calendars preference showing all calendars available in phone and after choosing the Calendars option it shows all calendars to select what user wants.

Can someone helps me here and shares some knowledge that how to do this.

Thanks

Was it helpful?

Solution 2

Fortunately I have found some help from developer site and able to get all available calendars and show them with dynamic Checkpreferences.

hope my code will help some one in future.

CalendarPreference.java

public class CalendarPreference extends PreferenceActivity{
private static final String CALENDAR_ID = "calendarId";
private static final String[] PROJECTION = new String[] { Calendars._ID,
        Calendars.CALENDAR_DISPLAY_NAME, Calendars.CALENDAR_COLOR };
private Set<String> initialActiveCalendars;

CheckBoxPreference mCheckBoxPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.calendaraccounts);
    SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
    initialActiveCalendars = prefs.getStringSet("PREF_ACTIVE_CALENDARS", null);
    populatePreferenceScreen(initialActiveCalendars);


}

private void populatePreferenceScreen(Set<String> activeCalendars) {
    Cursor cursor = createLoadedCursor();
    if (cursor == null) {
        return;
    }
    for (int i = 0; i < cursor.getCount(); i++) {
        cursor.moveToPosition(i);
        CheckBoxPreference checkboxPref = new CheckBoxPreference(this);
        checkboxPref.setTitle(cursor.getString(1));
        checkboxPref.setIcon(createDrawable(cursor.getInt(2)));
        int calendarId = cursor.getInt(0);
        checkboxPref.getExtras().putInt(CALENDAR_ID, calendarId);
        checkboxPref.setChecked(activeCalendars == null
                || activeCalendars.contains(String.valueOf(calendarId)));
        getPreferenceScreen().addPreference(checkboxPref);
    }
}

    @Override
       public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        return true;
    }


    private Cursor createLoadedCursor() {
    Uri.Builder builder = Calendars.CONTENT_URI.buildUpon();
    ContentResolver contentResolver = getContentResolver();
    return contentResolver.query(builder.build(), PROJECTION, null, null, null);
   }

    @Override
    public void onPause() {
        super.onPause();        
        HashSet<String> selectedCalendars = getSelectedCalenders();  
        if (!selectedCalendars.equals(initialActiveCalendars)) {
            persistSelectedCalendars(selectedCalendars);
            Log.v("Selected Calendars", selectedCalendars.toString());
            NewWidget.updateAllWidgets(this);
        }
    }

    private void persistSelectedCalendars(HashSet<String> prefValues) {
        SharedPreferences prefs = getPreferenceManager().getSharedPreferences();
        Editor editor = prefs.edit();
        editor.putStringSet("PREF_ACTIVE_CALENDARS", prefValues);
        editor.commit();
    }

    private HashSet<String> getSelectedCalenders() {
        PreferenceScreen preferenceScreen = getPreferenceScreen();
        int prefCount = preferenceScreen.getPreferenceCount();
        HashSet<String> prefValues = new HashSet<String>();
        for (int i = 0; i < prefCount; i++) {
            Preference pref = preferenceScreen.getPreference(i);
            if (pref instanceof CheckBoxPreference) {
                CheckBoxPreference checkPref = (CheckBoxPreference) pref;
                if (checkPref.isChecked()) {
                    prefValues.add(String.valueOf(checkPref.getExtras().getInt(CALENDAR_ID)));
                }
            }
        }
        return prefValues;
    }

    private Drawable createDrawable(int color) {
        Drawable drawable = getResources().getDrawable(R.drawable.prefs_calendar_entry);
        drawable.setColorFilter(new LightingColorFilter(0x0, color));
        return drawable;
    }


}

And here res/xml/calendaraccounts.xml

<PreferenceScreen>

</PreferenceScreen>

OTHER TIPS

To get all available calendars from mobile hope so this code will help.

Cursor cursor;

        if (android.os.Build.VERSION.SDK_INT <= 7) {
            cursor = getContentResolver().query(Uri.parse("content://calendar/calendars"), new String[] { "_id", "displayName" }, null,
                    null, null);

        }

        else if (android.os.Build.VERSION.SDK_INT <= 14) {
            cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),
                    new String[] { "_id", "displayName" }, null, null, null);

        }

        else {
            cursor = getContentResolver().query(Uri.parse("content://com.android.calendar/calendars"),
                    new String[] { "_id", "calendar_displayName" }, null, null, null);

        }

        // Get calendars name
        Log.i("@calendar","Cursor count " + cursor.getCount());
        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            String[] calendarNames = new String[cursor.getCount()];
            // Get calendars id
            int calendarIds[] = new int[cursor.getCount()];
            for (int i = 0; i < cursor.getCount(); i++) {
                calendarIds[i] = cursor.getInt(0);
                calendarNames[i] = cursor.getString(1);
                Log.i("@calendar","Calendar Name : " + calendarNames[i]);
                cursor.moveToNext();
            }
        } else {
            Log.e("@calendar","No calendar found in the device");
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top