0

I have added a calendar view in a dialog box. The dialog box appears on a button click. The problem I faced is that, when the dialog box appears, the calendar view does not display current month and year on top. But when I changed the month, the month and year on top appears (for current month also). I want to show this month and year on initial dialog show. Please help me on this. Thanks in advance.

I haven't enough reputation to post a screen shot. Sorry for this.

This is the code :

date.setOnClickListener(new OnClickListener() {         
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
                      (Context.LAYOUT_INFLATER_SERVICE);
            RelativeLayout ll= (RelativeLayout)inflater.inflate(R.layout.calendar_dialog, null, false);
            final CalendarView cv = (CalendarView) ll.getChildAt(0);
            cv.setOnDateChangeListener(new OnDateChangeListener() {
                @Override
                public void onSelectedDayChange(CalendarView view, int year, int month,
                        int dayOfMonth) {
                    // TODO Auto-generated method stub
                    String date = dayOfMonth+"/"+(month+1)+"/"+year;
                    filterNews(date);
                }
            });

            AlertDialog.Builder builder = new AlertDialog.Builder(FirstView.this, AlertDialog.THEME_HOLO_DARK);
            builder.setTitle("News Calendar")
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Do nothing.
                    }
                }
            );
            AlertDialog alertDialog = builder.create();
            alertDialog.show();         
        }
    });

date is a Button.

poly
  • 13
  • 1
  • 5

3 Answers3

0
final Calendar c = Calendar.getInstance();
        maxyear = c.get(Calendar.YEAR);
        maxmonth = c.get(Calendar.MONTH);
        maxday = c.get(Calendar.DAY_OF_MONTH);


DatePickerDialog _date =   new DatePickerDialog(this, datePickerListener, maxyear,maxmonth,
                    maxday)
1234567
  • 2,226
  • 4
  • 24
  • 69
0

Just a short example of a working CalendarView that you can apply to your Dialog:

CalendarView calendar;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   calendar = (CalendarView) findViewById(R.id.calendar);
   calendar.setOnDateChangeListener(new OnDateChangeListener() {

      @Override
      public void onSelectedDayChange(CalendarView view,
      int year, int month, int dayOfMonth) {
      Toast.makeText(getApplicationContext(), dayOfMonth +"/"+month+"/"+ year,Toast.LENGTH_LONG).show();}});
}
Kody
  • 1,154
  • 3
  • 14
  • 31
  • Thanks for your answer. Let me check it. – poly Jan 08 '15 at 13:11
  • I am using min SDK 14. – poly Jan 08 '15 at 14:10
  • I'm perplexed right now :) Actually this works. Do you have a github oder gitlab account or something else where you could upload the entire application? I would check it out and have a closer look then. – Kody Jan 08 '15 at 15:07
  • Sorry to say... I can't share the source code of my application because it belongs to my company. – poly Jan 09 '15 at 05:48
0
   <CalendarView
    android:id="@+id/calendar"
    android:layout_margin="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Use in Xml File And in manifest you should change

android:minSdkVersion="11"

Callendar view requires api 11 :)