Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display google calendar on Android calendarView widget?

My question is simple, is there a way to display a Google Calendar using Android calendarView inside an application? I can't find a way to do so

like image 900
lamp ard Avatar asked Aug 14 '12 19:08

lamp ard


1 Answers

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Introducing ICS Calendar"
android:gravity="center"
 android:padding="10dip"/>
<TextView
android:id="@+id/data"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="center"
 android:padding="10dip"/>
<LinearLayout
android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/previous"
 android:text="Prev"
 android:padding="10dip"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next"
 android:text="Next"
 android:padding="10dip"/>
</LinearLayout>
</LinearLayout>

Main.java

import java.text.Format;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity implements OnClickListener{

private Cursor mCursor = null;
private static final String[] COLS = new String[]
{ CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART};
}

Now we need to override the on create method. Pay special attention to how we populate the database cursor. This is where we need our previously defined COLS constant. You’ll note also that after the database cursor is initialized and the click handler callbacks are set, we go ahead and manually invoke the on click handler. This shortcut allows us to initially fill out our UI without having to repeat code.

Main.java

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mCursor = getContentResolver().query(
CalendarContract.Events.CONTENT_URI, COLS, null, null, null);
mCursor.moveToFirst();
Button b = (Button)findViewById(R.id.next);
b.setOnClickListener(this);
b = (Button)findViewById(R.id.previous);
b.setOnClickListener(this);
onClick(findViewById(R.id.previous));
}

In our callback, we will manipulate the cursor to the correct entry in the database and update the UI.

@Override
public void onClick(View v) {
TextView tv = (TextView)findViewById(R.id.data);
String title = "N/A";
Long start = 0L;
switch(v.getId()) {
case R.id.next:
if(!mCursor.isLast()) mCursor.moveToNext();
break;
case R.id.previous:
if(!mCursor.isFirst()) mCursor.moveToPrevious();
break;
}
Format df = DateFormat.getDateFormat(this);
Format tf = DateFormat.getTimeFormat(this);
try {
title = mCursor.getString(0);
start = mCursor.getLong(1);
} catch (Exception e) {
//ignore
}
tv.setText(title+" on "+df.format(start)+" at "+tf.format(start));
}

permission :

<uses-permission android:name="android.permission.READ_CALENDAR"/>
like image 88
ckpatel Avatar answered Sep 20 '22 07:09

ckpatel