CalendarView Clickable Android

Ken picture Ken · Aug 14, 2012 · Viewed 32.1k times · Source

I am trying to start a new activity when you click on a date in CalendarView but my event doesn't seem to fire. I have set clickable to true and specified an onclick (both in xml)

this is the xml:

     <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" >

     <CalendarView
     android:id="@+id/calendarView1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_alignParentLeft="true"
     android:layout_alignParentTop="true"
     android:clickable="true"      
     android:onClick="CalendarClick" />

     </RelativeLayout>

this is my code for the calendar activity:

    public class CalendarActivity extends Activity 
    {


      @Override
      public void onCreate(Bundle savedInstanceState) 
      {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_calendar);
      }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) 
     {
      getMenuInflater().inflate(R.menu.activity_calendar, menu);
      return true;
     }

     public void CalendarClick(View view)
     {
      Intent myIntent = new Intent(CalendarActivity.this, MainActivity.class);
      CalendarActivity.this.startActivity(myIntent);
     }      
  }

Answer

Atul Bhardwaj picture Atul Bhardwaj · Aug 14, 2012

This works fine.....

 public class MyActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         setContentView(R.layout.newact);
        CalendarView calendarView=(CalendarView) findViewById(R.id.calendarView1);
        calendarView.setOnDateChangeListener(new OnDateChangeListener() {

            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month,
                    int dayOfMonth) {
                 Toast.makeText(getApplicationContext(), ""+dayOfMonth, 0).show();// TODO Auto-generated method stub

            }
        });
    }

And this is xml

<?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" >

    <CalendarView
        android:id="@+id/calendarView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"  
        />

</RelativeLayout>