web-dev-qa-db-fra.com

Calendrier Android personnalisé

Je cherche un CalendarView qui peut être utilisé dans une boîte de dialogue comme ceci:

Je prévois d’utiliser Android.widget.CalendarView mais il est disponible à partir de l’API de niveau 11. C’est un problème, étant donné qu’il ya encore beaucoup d’utilisateurs utilisant Android 2.3.3 (mais aucun des principaux ne peut lâcher ces utilisateurs).

Laquelle de ces options devrais-je utiliser?

1) les bibliothèques disponibles et les utiliser en conséquence.

2) personnaliser le Android.widget.CalendarView

Si n ° 2, existe-t-il des exemples?

24
sankettt

Salut visiter tous les liens donnés, espère vous aider

1 . https://github.com/inteist/Android-better-time-picker

2 . https://github.com/derekbrameyer/Android-betterpickers

3 . http://www.androiddevelopersolutions.com/2013/05/Android-calendar-sync.html

4 . http://www.androidviews.net/2013/04/extendedcalendarview/

5 . http://abinashandroid.wordpress.com/2013/07/21/how-to-create-custom-calendar-in-Android/

6 . http://w2davids.wordpress.com/Android-simple-calendar/

7 . https://github.com/roomorama/Caldroid

8 . https://github.com/flavienlaurent/datetimepicker

quand vous visiterez

_ { https://github.com/derekbrameyer/Android-betterpickers } _

Pour une implémentation fonctionnelle de ce projet, voir exemple/dossier.

Implémentez les rappels appropriés de Handler:

public class MyActivity extends Activity implements DatePickerDialogFragment.DatePickerDialogHandler {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    // ...
  }

  @Override
  public void onDialogDateSet(int year, int monthOfYear, int dayOfMonth) {
    // Do something with your date!
  }
}

Utilisez l'une des classes Builder pour créer un PickerDialog avec un thème:

DatePickerBuilder dpb = new DatePickerBuilder()
    .setFragmentManager(getSupportFragmentManager())
    .setStyleResId(R.style.BetterPickersDialogFragment);
dpb.show();

aussi pour un autre exemple, vous pouvez visiter

https://github.com/roomorama/Caldroid

et utiliser comme suit

Pour intégrer le fragment caldroid à votre activité, utilisez le code ci-dessous:

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 = getSupportFragmentManager().beginTransaction();
t.replace(R.id.calendar1, caldroidFragment);
t.commit();
49
Jitesh Upadhyay

espérons que cela vous aidera tout cela est un calendrier personnalisé éditable que vous pouvez modifier à votre façon 

prendre des fragments 

public class fragmentCalender extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

ArrayList<calenderModelCustom> itemsArrayList = new ArrayList<>(); // calls function to get items list

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;
private View mView;
private Context mContext;
private GridView mGridView;
private int temp = 1;
private calenderModelCustom mCalenderModelCustom;
private ImageButton mPreviousButton, mForwardButton;
private int mPageCount = 0;
private TextView mYearLable;
private int mYear;
private Calendar mCalender;
private int mCurrentDay, mCurrentMonth, mCurrentYear;
private ArrayList<calenderModelCustom> list;
private String mFirstDate;

public fragmentCalender() {
    // Required empty public constructor
}

/**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @param param1 Parameter 1.
 * @param param2 Parameter 2.
 * @return A new instance of fragment fragmentCalender.
 */
// TODO: Rename and change types and number of parameters
public static fragmentCalender newInstance(String param1, String param2) {
    fragmentCalender fragment = new fragmentCalender();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    mView = inflater.inflate(R.layout.fragment_fragment_calender2, container, false);
    mContext = this.getActivity();
    Util.CAL_CUR_DATE_CNST = "0";
    mCalender = Calendar.getInstance();
    list = new ArrayList<>();
    //initialize xml element object
    initializeView();
    //current things
    setCurrentEnv(mCalender.get(Calendar.MONTH));
    //current date
    getDateTime();
    //actions
    initializeViewAction();
    //set adapter
    initializeViewAdapter(mPageCount, Util.CAL_CUR_DATE_CNST);


    return mView;
}

/*
* setting first date,day of current month
* */
private void setCurrentEnv(int mCurrentMonth){
    Log.i("<<>>Cal",String.valueOf(mCurrentMonth));
    Date date = new Date();
    date.setDate(1);
    date.setMonth(mCurrentMonth);
    date.setYear(mCurrentYear);
    mFirstDate = getDay(date);
    Log.i("<<>>Cal",mFirstDate);
    if (mFirstDate.contains("Fri")) {
        mPageCount = 4;
        initializeViewAction();
        initializeViewAdapter(mPageCount, Util.CAL_CUR_DATE_CNST);
    } else if (mFirstDate.contains("Sat")) {
        mPageCount = 5;
        initializeViewAction();
        initializeViewAdapter(mPageCount, Util.CAL_CUR_DATE_CNST);
    } else if (mFirstDate.contains("Sunday")) {
        mPageCount = 6;
        initializeViewAction();
        initializeViewAdapter(mPageCount, Util.CAL_CUR_DATE_CNST);
    } else if (mFirstDate.contains("Mon")) {
        mPageCount = 0;
        initializeViewAction();
        initializeViewAdapter(mPageCount, Util.CAL_CUR_DATE_CNST);
    } else if (mFirstDate.contains("Tue")) {
        mPageCount = 1;
        initializeViewAction();
        initializeViewAdapter(mPageCount, Util.CAL_CUR_DATE_CNST);
    } else if (mFirstDate.contains("Wed")) {
        mPageCount = 2;
        initializeViewAction();
        initializeViewAdapter(mPageCount, Util.CAL_CUR_DATE_CNST);
    } else if (mFirstDate.contains("Thu")) {
        mPageCount = 3;
        initializeViewAction();
        initializeViewAdapter(mPageCount, Util.CAL_CUR_DATE_CNST);
    }
}

private void initializeViewAction() {

    if (mCurrentMonth == 1) {
        mYearLable.setText("Jan " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 2) {
        mYearLable.setText("Feb " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 3) {
        mYearLable.setText("Mar " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 4) {
        mYearLable.setText("Apr " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 5) {
        mYearLable.setText("May " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 6) {
        mYearLable.setText("Jun " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 7) {
        mYearLable.setText("Jul " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 8) {
        mYearLable.setText("Aug " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 9) {
        mYearLable.setText("Sep " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 10) {
        mYearLable.setText("Oct " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 11) {
        mYearLable.setText("Nov " + String.valueOf(mCurrentYear));
    } else if (mCurrentMonth == 12) {
        mYearLable.setText("Dec " + String.valueOf(mCurrentYear));
    } else {
        mYearLable.setText("" + String.valueOf(mCurrentYear));
    }
    mForwardButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //increment current month by one
            mCurrentMonth++;

            //if current month >= 13 then start from first month
            if (mCurrentMonth >= 13) {
                mCurrentMonth = 1;
                mCurrentYear ++;
            }
            setIfBackOrForwardToCurrentMonth();
            setCurrentEnv(mCurrentMonth);
        }
    });

    mPreviousButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //increment current month by one
            mCurrentMonth--;

            //if current month >= 13 then start from first month
            if (mCurrentMonth < 1) {
                mCurrentMonth = 12;
                mCurrentYear --;
            }
            setIfBackOrForwardToCurrentMonth();
            setCurrentEnv(mCurrentMonth);
        }
    });

    mGridView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
        public void onSwipeTop() {
            Log.i("top","");
        }
        public void onSwipeRight() {
            //increment current month by one
            mCurrentMonth++;

            //if current month >= 13 then start from first month
            if (mCurrentMonth >= 13) {
                mCurrentMonth = 1;
                mCurrentYear ++;
            }
            setIfBackOrForwardToCurrentMonth();
            setCurrentEnv(mCurrentMonth);
        }
        public void onSwipeLeft() {
            //increment current month by one
            mCurrentMonth--;

            //if current month >= 13 then start from first month
            if (mCurrentMonth < 1) {
                mCurrentMonth = 12;
                mCurrentYear --;
            }
            setIfBackOrForwardToCurrentMonth();
            setCurrentEnv(mCurrentMonth);
        }
        public void onSwipeBottom() {
            Log.i("bottom","");
        }

    });



}

/*
* parameter to set current date marker on calender
* */
private void setIfBackOrForwardToCurrentMonth() {
    Log.i("<<>>Cal<>",mCurrentMonth+"  "+Util.CURRENT_DATE);
    if (mCurrentMonth==mCalender.get(Calendar.MONTH)+1){
        Util.CAL_CUR_DATE_CNST = "0";
    } if (mCurrentMonth!=mCalender.get(Calendar.MONTH)+1){
        Util.CAL_CUR_DATE_CNST = "1";
    }
}

/*method to get current date and store it separetely in variable*/
private String getDateTime() {
    DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Date date = new Date();
    mCurrentDay = mCalender.get(Calendar.DATE);
    mCurrentMonth = mCalender.get(Calendar.MONTH)+1;
    Util.CURRENT_DATE = String.valueOf(mCalender.get(Calendar.DATE));
    Log.i("<<>>Cal<>",Util.CURRENT_DATE);
    mCurrentYear = mCalender.get(Calendar.YEAR);
    return dateFormat.format(date);
}

/*
* current day to print in text format
* */
public static String getDay(Date date) {
    SimpleDateFormat simpleDateformat = new SimpleDateFormat("EEEE"); // the day of the week spelled out completely
    System.out.println(simpleDateformat.format(date));

    return simpleDateformat.format(date);
}

private void initializeViewAdapter(int temp , String mVisibility) {

    CalenderAdapterView mCalenderAd = new CalenderAdapterView(mContext, generateItemsList(temp),Util.CURRENT_DATE,mVisibility);
    mGridView.setAdapter(mCalenderAd);

}

private ArrayList<calenderModelCustom> generateItemsList(int temp) {
    list.clear();
    String itemNames[] = getResources().getStringArray(R.array.items_name);

    for (int i = 0; i < temp; i++) {
        list.add(new calenderModelCustom("", ""));
    }


    for (int i = 0; i < itemNames.length; i++) {
        list.add(new calenderModelCustom(itemNames[i], itemNames[i]));
    }

    return list;
}

private void initializeView() {
    mYearLable = (TextView) mView.findViewById(R.id.currentDateLabel);
    mForwardButton = (ImageButton) mView.findViewById(R.id.btn_forward);
    mPreviousButton = (ImageButton) mView.findViewById(R.id.btn_previous);

    mGridView = (GridView) mView.findViewById(R.id.cal_grid_view);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
    }
    }

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

/**
 * This interface must be implemented by activities that contain this
 * fragment to allow an interaction in this fragment to be communicated
 * to the activity and potentially other fragments contained in that
 * activity.
 * <p>
 * See the Android Training lesson <a href=
 * "http://developer.Android.com/training/basics/fragments/communicating.html"
 * >Communicating with Other Fragments</a> for more information.
 */
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}
}

définir la vue grille en xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
xmlns:app="http://schemas.Android.com/apk/res-auto">

<Android.support.v7.widget.CardView
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:layout_margin="@dimen/marginRight"
    >
    <RelativeLayout
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content">
    <Android.support.constraint.ConstraintLayout
        Android:id="@+id/calendarHeader"
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content">

        <ImageButton
            Android:id="@+id/btn_previous"
            style="@style/Widget.AppCompat.Button.Borderless.Colored"
            Android:layout_width="85dp"
            Android:layout_height="0dp"
            Android:src="@drawable/ic_action_date_back"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            Android:id="@+id/btn_forward"
            style="@style/Widget.AppCompat.Button.Borderless.Colored"
            Android:layout_width="85dp"
            Android:layout_height="0dp"
            Android:src="@drawable/ic_action_date_next"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            Android:id="@+id/currentDateLabel"
            Android:layout_width="0dp"
            Android:layout_height="56dp"
            Android:gravity="center"
            Android:text=""
            Android:textSize="18sp"
            app:layout_constraintLeft_toRightOf="@id/btn_previous"
            app:layout_constraintRight_toLeftOf="@id/btn_forward"
            app:layout_constraintTop_toTopOf="parent" />


    </Android.support.constraint.ConstraintLayout>

    <!-- eventDays header -->
    <LinearLayout
        Android:id="@+id/calendar_header"
        Android:layout_width="match_parent"
        Android:layout_height="40dp"
        Android:layout_below="@id/calendarHeader"
        Android:gravity="center_vertical"
        Android:orientation="horizontal">

        <TextView
            Android:id="@+id/mondayLabel"
            Android:layout_width="0dp"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:gravity="center_horizontal"
            Android:text="@string/monday"
            Android:textColor="@color/daysLabelColor" />

        <TextView
            Android:id="@+id/tuesdayLabel"
            Android:layout_width="0dp"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:gravity="center_horizontal"
            Android:text="@string/tuesday"
            Android:textColor="@color/daysLabelColor" />

        <TextView
            Android:id="@+id/wednesdayLabel"
            Android:layout_width="0dp"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:gravity="center_horizontal"
            Android:text="@string/wednesday"
            Android:textColor="@color/daysLabelColor" />

        <TextView
            Android:id="@+id/thursdayLabel"
            Android:layout_width="0dp"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:gravity="center_horizontal"
            Android:text="@string/thursday"
            Android:textColor="@color/daysLabelColor" />

        <TextView
            Android:id="@+id/fridayLabel"
            Android:layout_width="0dp"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:gravity="center_horizontal"
            Android:text="@string/friday"
            Android:textColor="@color/daysLabelColor" />

        <TextView
            Android:id="@+id/saturdayLabel"
            Android:layout_width="0dp"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:gravity="center_horizontal"
            Android:text="@string/saturday"
            Android:textColor="@color/daysLabelColor" />

        <TextView
            Android:id="@+id/sundayLabel"
            Android:layout_width="0dp"
            Android:layout_height="wrap_content"
            Android:layout_weight="1"
            Android:gravity="center_horizontal"
            Android:text="@string/sunday"
            Android:textColor="@color/daysLabelColor" />
    </LinearLayout>

    <GridView
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:numColumns="7"
        Android:gravity="center_vertical"
        Android:layout_below="@id/calendar_header"
        Android:id="@+id/cal_grid_view">

    </GridView>
    </RelativeLayout>
</Android.support.v7.widget.CardView>

créer Util.class et écrire des constantes

public static String CAL_CUR_DATE_CNST ;
public static String CURRENT_DATE ;

créer un adaptateur pour la vue grille 

public class CalenderAdapterView extends BaseAdapter {
Calendar mCalender = Calendar.getInstance();
private Context context; //context
private ArrayList<calenderModelCustom> items; //data source of the list adapter
private String mCurrentDate;
private String mVisibility;
//public constructor
public CalenderAdapterView(Context context, ArrayList<calenderModelCustom> items, String currentDate, String mVisibility) {
    this.context = context;
    this.items = items;
    this.mCurrentDate = currentDate;
    this.mVisibility = mVisibility;
}

@Override
public int getCount() {
    //returns total of items in the list
    return items.size(); 
}

@Override
public Object getItem(int position) {
    //returns list item at the specified position
    return items.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // inflate the layout for each list row
    if (convertView == null) {
        convertView = LayoutInflater.from(context).
                inflate(R.layout.calendar_item, parent, false);
    }

    // get current item to be displayed
    calenderModelCustom currentItem = (calenderModelCustom) getItem(position);


    // get the TextView for item name and item description
    TextView textViewItemName = (TextView)
            convertView.findViewById(R.id.text_view_item_name);
    ImageView mImageBaground = (ImageView)
            convertView.findViewById(R.id.img_baground);

    //sets the text for item name and item description from the current item object
    textViewItemName.setText(currentItem.getmNoOfDays());
    if (mVisibility.equals("0")) {
        if (String.valueOf(mCalender.get(Calendar.DATE)).equals(currentItem.getmNoOfDays())) {
            mImageBaground.setImageResource(R.drawable.circle_cur_date);
        }
    }

    // returns the view for the current row
    return convertView;
}
}

créer un fichier xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:orientation="vertical">

<RelativeLayout
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">
    <ImageView
    Android:layout_width="25dp"
    Android:layout_height="25dp"
        Android:layout_centerHorizontal="true"
        Android:layout_centerVertical="true"
    Android:id="@+id/img_baground"/>

    <TextView
        Android:id="@+id/text_view_item_name"
        Android:layout_width="match_parent"
        Android:layout_centerHorizontal="true"
        Android:gravity="center_horizontal"
        Android:layout_centerVertical="true"
        Android:text="1"
        Android:layout_height="wrap_content" />

</RelativeLayout>
<TextView
    Android:id="@+id/text_view_item_description"
    Android:layout_width="match_parent"
    Android:gravity="center_horizontal"
    Android:layout_height="wrap_content" />
 </LinearLayout>

et un fichier extractible

<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:Android="http://schemas.Android.com/apk/res/Android"
Android:shape="oval">

<size
    Android:width="30dp"
    Android:height="30dp"
    />

<stroke Android:width="0.5dp"
    Android:color="@color/monsoon"/>

</shape>

et créer une classe 

public class calenderModelCustom {

private String mStartPos;

public String getmStartPos() {
    return mStartPos;
}

public void setmStartPos(String mStartPos) {
    this.mStartPos = mStartPos;
}

public String getmNoOfDays() {
    return mNoOfDays;
}

public void setmNoOfDays(String mNoOfDays) {
    this.mNoOfDays = mNoOfDays;
}

private String mNoOfDays;

public calenderModelCustom (String mStartPos,String mNoOfDays){
    this.mStartPos = mStartPos;
    this.mNoOfDays = mNoOfDays;
}
}

ajouter dans string.xml

<string name="monday">M</string>
<string name="tuesday">T</string>
<string name="wednesday">W</string>
<string name="thursday">T</string>
<string name="friday">F</string>
<string name="saturday">S</string>
<string name="sunday">S</string>

//calender
<string-array name="items_name">
    <item>1</item>
    <item>2</item>
    <item>3</item>
    <item>4</item>
    <item>5</item>
    <item>6</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
    <item>11</item>
    <item>12</item>
    <item>13</item>
    <item>14</item>
    <item>15</item>
    <item>16</item>
    <item>17</item>
    <item>18</item>
    <item>19</item>
    <item>20</item>
    <item>21</item>
    <item>22</item>
    <item>23</item>
    <item>24</item>
    <item>25</item>
    <item>26</item>
    <item>27</item>
    <item>28</item>
    <item>29</item>
    <item>30</item>
    <item>31</item>

</string-array>

ajouter une dépendance pour la carte

compile 'com.Android.support:cardview-v7:26.1.0' 

écrire la portée de la mise en œuvre les gars

0
Mahesh Pandit