简体   繁体   中英

Android CalendarView not displaying the days

I have been using Android CalendarView to get a Calendar displayed on my screen in a Appointment Manager Application. In my case so far, the app user will be able to create an appointment by selecting the Create an appointment button and I intend to display a Calendar to select a date along with three or four text inputs. But I tend to always get only the CalendarView month, year and the headings of the week's days but not the list of days of the month. I am confused whether it is an issue related to the layout I am using or whether it is some other reason. The following are the source code of the related class and layout file.

CreateAppointmentActivity.java

package lk.iit.appointmentmanagerapp.activities;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CalendarView;
import android.widget.Toast;
import android.widget.CalendarView.OnDateChangeListener;

public class CreateAppointmentActivity extends Activity {

    CalendarView calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_appointment);
        this.initializeCalendar();  
    }

    public void initializeCalendar() {
        calendar = (CalendarView) findViewById(R.id.calendar);

        calendar.setShowWeekNumber(false);

        calendar.setFirstDayOfWeek(2);

        calendar.setOnDateChangeListener(new OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month, int day) {
                Toast.makeText(getApplicationContext(), day + "/" + month + "/" + year, Toast.LENGTH_LONG).show();
            }
        });
    }

}

activity_create_appointment.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/background"
    tools:context="lk.iit.appointmentmanagerapp.activities.CreateAppointmentActivity" >

    <TextView
       android:id="@+id/create_activity_title"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/welcome_create"
       android:layout_centerHorizontal="true"
       android:layout_marginTop="15dip"
       android:layout_marginBottom="25dip"
       android:textSize="18.5sp"
       android:textColor="#ffffff" />

    <CalendarView
      android:id="@+id/calendar"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_below="@+id/create_activity_title"
        />

    <TextView
      android:id="@+id/title_textview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/calendar"
      android:layout_marginTop="15dip"
      android:text="@string/title_textview_text"
      android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
       android:id="@+id/edit_title"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/calendar"
       android:layout_marginTop="15dip"
       android:layout_toRightOf="@+id/title_textview"
       android:layout_marginLeft="30dip"
       android:background="#ffffff"
       android:ems="10"
       android:inputType="text" >
       <requestFocus />
   </EditText>

    <TextView
      android:id="@+id/time_textview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/title_textview"
      android:layout_marginTop="25dip"  
      android:text="@string/time_textview_text"
      android:textAppearance="?android:attr/textAppearanceMedium" 
      />

   <EditText
      android:id="@+id/edit_time"
      android:background="#ffffff"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_toRightOf="@+id/time_textview"
      android:layout_below="@+id/edit_title"
      android:layout_marginLeft="25dip"
      android:layout_marginTop="25dip"  
      android:ems="10"
      android:inputType="time" />

   <TextView
      android:id="@+id/details_textview"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/time_textview"
      android:text="@string/details_textview_text"
      android:layout_marginTop="25dip"
      android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/edit_details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/details_textview"
        android:layout_alignBottom="@+id/details_textview"
        android:layout_alignLeft="@+id/edit_title"
        android:background="#ffffff"
        android:ems="10"
        android:inputType="text"
        android:scrollbars="vertical" >
    </EditText>

    <Button 
        android:id="@+id/save_new_appointment_button" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/details_textview"
        android:layout_marginTop="15dip"
        android:layout_marginBottom="10dip"
        android:layout_centerHorizontal="true"
        android:text="@string/save_button_text"
        android:textAllCaps="false" />
    <Button
        android:id="@+id/reset_button"  
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/save_new_appointment_button"
        android:layout_marginTop="2dip"
        android:layout_centerHorizontal="true"
        android:text="@string/reset_button_text"
        android:textAllCaps="false" />

</RelativeLayout>

I am relatively new to Android development and its user interface designing. Can anybody please suggest me any solutions for the above case?

change the layout parameters to match_parent

<CalendarView
  android:id="@+id/calendar"
  android:layout_width="match_parent" 
  android:layout_height="match_parent"
  android:layout_centerHorizontal="true"
  android:layout_below="@+id/create_activity_title"
    />

I fix it on an old Samsung on Kitkat with minHeight

<CalendarView
                android:id="@+id/fromCalendarView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_weight="50"
                android:minHeight="300dp"/>

Set minHeight dont work. Bellow works

            <CalendarView
                android:id="@+id/calendarView"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:layout_below="@+id/toolbar" />

Above Answer worked for me.

<CalendarView
android:id="@+id/calendarView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@+id/toolbar" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM