Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CalendarView not showing days of the month

I'm doing a calendar app and I encountered a strange problem.
If I'm not wrong adding a CalendarView to my layout should let me see the days of the month.
So I made a new project where the only thing I did was placing the CalendarView on my MainActivity layout.
The problem is that once I run the app I can see only this and there is no error.

package com.example.kanye.kalendarz22;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CalendarView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
CalendarView calendar ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.initializeCalendar();

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}
public void initializeCalendar() {
    calendar = (CalendarView) findViewById(R.id.calendarView);

    calendar.setShowWeekNumber(false);

    calendar.setFirstDayOfWeek(2);

    calendar.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
        @Override
        public void onSelectedDayChange(CalendarView view, int year, int month, int day) {

            Toast.makeText(MainActivity.this, day + "/" + month + "/" + year, Toast.LENGTH_SHORT).show();

        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

http://i.imgur.com/iJsEwCc.png?1

http://i.imgur.com/AdciraO.png?1

XML code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main" tools:context=".MainActivity">

<TextView android:text="Hello World!" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

<CalendarView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/calendarView"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

Any advice what to do to make it displayed correctly (with the particular days of the month)? I was looking for permissions to add to manifest, but apparently it doesn't need any. By the way I'm completely aware that I need the min Api level 11 to use this CalendarView.

Edit: Sorry for not giving you enough information, at the cause: lack of experience and my bad English is limiting me.

like image 656
Enroky Avatar asked Dec 23 '15 15:12

Enroky


2 Answers

After more than a year you probably found a solution or used something else to get rid of the problem. But as I was trying different things to get rid of this strange issue, I managed to find out what's happening.

Starting from API 20, CalendarView was renewed and looks like in your designer screenshot. It's still the case for the most recent API 25.

But for API 19 (and lower I suppose), it looks completely different :

CalendarView for Android 4.4 (API 19)

And as silly as it seems, the problem comes from the axml layout. This calendar is absolutely unable to handle wrap_content parameter for the calendar's height. Putting any value in pixels or dp will fix the issue.

So, what you can do is add this in your code to change the height of the calendar view for API 19 and lower (I'm using Xamarin so it's C# but it can easily be translated to java) :

if (((int)Android.OS.Build.VERSION.SdkInt) <= 19) {
    var parameters = myCalendarView.LayoutParameters;
    parameters.Height = 500; //value in pixels
}

It solved the problem for me. Sadly this kind of thing is frequent between API 19 and higher APIs.. I would abandon completely support for this API and lower but according to Android stats, there's still 17% of working devices on Android 4.4.. We'll have to deal with it I guess.

like image 163
YumeYume Avatar answered Nov 08 '22 01:11

YumeYume


I have the same issue, as far as I understand it, it has to do with API version.
The design view is showing you API version 23, while your phone/emulator is lower, <20.
I'm not sure if this can be fixed at all.

Try using instead, DatePicker, adding these two lines:

datePicker.setCalendarViewShown(true);
datePicker.setSpinnerShown(false)

But the fun doesn't stop there, the onDateChanged function is not called in Android 5.0 for datePicker... so have both and remove the irrelevant one is what I did to support every possibility.

like image 33
Max Izrin Avatar answered Nov 08 '22 03:11

Max Izrin