Aligator Aligator - 1 month ago 18
Android Question

Android calendar (caldroid) create 2 calendar views

Hi I'm using Caldroid calendar, and I use initialization code from official site only I use getFragmentManager() instead getSupportFragmentManager() becouse I use code in class which extend from Fragment not Activity. MysTab is fragment of TabbedActivity. In result I have 2 calendars and I do not know why. Any ideas?

screenshot

public class MysTab extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.logs_tab, container, false);

CaldroidFragment caldroidFragment = new CaldroidFragment();
Bundle args = new Bundle();
Calendar cal = Calendar.getInstance();
args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
caldroidFragment.setArguments(args);

FragmentTransaction t = getFragmentManager().beginTransaction();
t.replace(R.id.caldroidCal, caldroidFragment);
t.commit();



return rootView;
}
}


Layout:

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

<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.roomorama.caldroid.CaldroidFragment"
android:id="@+id/caldroidCal"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />

</LinearLayout>

Answer

You need to decide whether you want to add a calendar dynamic or use what a layout gives to you.

  1. If you want to add a calendar dynamic then your code should look like this:

    public class MysTab extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
               return inflater.inflate(R.layout.logs_tab, container, false);
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
               CaldroidFragment caldroidFragment = new CaldroidFragment();
    
               Bundle args = new Bundle();
               Calendar cal = Calendar.getInstance();
               args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
               args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
               caldroidFragment.setArguments(args);
    
               FragmentTransaction t = getChildFragmentManager().beginTransaction();
               t.replace(R.id.caldroidCal, caldroidFragment);
               t.commit();
        }
    
    }
    

    And in the same time your layout should be look like this:

       <LinearLayout
               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">
    
               <FrameLayout
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:id="@+id/caldroidCal" />
    
       </LinearLayout>
    
  2. If you want to add a calendar fragment static then you just need to create a fragment and inflate your layout. No need to create a fragment manually:

    public class MysTab extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
               return inflater.inflate(R.layout.logs_tab, container, false);
        }
    
    }
    

    And layout looks like that:

    <LinearLayout 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">
    
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.roomorama.caldroid.CaldroidFragment"
            android:id="@+id/caldroidCal" />
    
    </LinearLayout>