CalendarView Clickable Android
Asked Answered
G

3

7

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);
     }      
  }
Gosling answered 14/8, 2012 at 9:12 Comment(2)
check public void CalendarClick(View view) Toast something{ }Delicate
It only toasts when I click outside of the date and not on a specific dateGosling
S
19

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>
Selfmoving answered 14/8, 2012 at 11:33 Comment(2)
To me, it's only showing the week names. But it's not showing the dates of month.Colettecoleus
Hi, It works fine, but can it work api 9 and above...or any option.Unscramble
S
2

Use this to know which date has been clicked

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

        }
    });
Selfmoving answered 14/8, 2012 at 10:3 Comment(3)
This gives me errors like: Multiple markers at this line - Syntax error on token "setOnDateChangeListener", = expected after this token - Syntax error on token(s), misplaced construct(s) and The method onSelectedDayChange(CalendarView, int, int, int) of type CalendarActivity must override or implement a supertype methodGosling
Furthermore I have tried to find an onDateChanged in the xml and it doesn't seem like calenderView has got that propertyGosling
I think you forgot to add these line "import android.widget.CalendarView; import android.widget.CalendarView.OnDateChangeListener;"Selfmoving
D
0

OnGlobalLayoutListener will do the trick if you want to click already selected date.

calendarView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
            @Override
            public void onGlobalLayout()
            {
            //your code here
            }
        });
Dissolvent answered 6/8, 2015 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.