728x90
아래 깃허브 저장소에 있는 CompactCalendarView 라이브러리를 사용하여 달력 예제 만들어 보겠습니다.
선택한 날짜에 일정을 추가하고 삭제할 수 있습니다.
https://github.com/SundeepK/CompactCalendarView
1. dependencies 추가
dependencies {
implementation 'com.github.sundeepk:compact-calendar-view:3.0.0'
}
2. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView_month"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textAlignment="center"
android:background="#ffe95451"
android:textColor="#fff"
android:textSize="30sp"
android:gravity="center"
android:text="달력" />
<com.github.sundeepk.compactcalendarview.CompactCalendarView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/compactcalendar_view"
android:layout_width="match_parent"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_height="250dp"
app:compactCalendarTargetHeight="250dp"
app:compactCalendarTextSize="12sp"
app:compactCalendarBackgroundColor="#ffe95451"
app:compactCalendarTextColor="#fff"
app:compactCalendarCurrentSelectedDayBackgroundColor="#E57373"
app:compactCalendarCurrentDayBackgroundColor="#B71C1C"
app:compactCalendarMultiEventIndicatorColor="#fff"
/>
<TextView
android:id="@+id/textView_result"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textAlignment="center"
android:background="@color/purple_200"
android:textColor="#fff"
android:gravity="center"
android:hint="클릭한 날짜 일정보기" />
</LinearLayout>
3.
import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.util.Log;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.github.sundeepk.compactcalendarview.CompactCalendarView;
import com.github.sundeepk.compactcalendarview.domain.Event;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private SimpleDateFormat dateFormatForDisplaying = new SimpleDateFormat("yyyy-MM-dd", Locale.KOREA);
private SimpleDateFormat dateFormatForMonth = new SimpleDateFormat("yyyy년 MM월", Locale.KOREA);
private SimpleDateFormat dateFormatForMonth2 = new SimpleDateFormat("yyyy-MM", Locale.KOREA);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CompactCalendarView compactCalendarView = (CompactCalendarView) findViewById(R.id.compactcalendar_view);
TextView textView_month = (TextView) findViewById(R.id.textView_month);
TextView textView_result = (TextView) findViewById(R.id.textView_result);
textView_month.setText(dateFormatForMonth.format(compactCalendarView.getFirstDayOfCurrentMonth()));
compactCalendarView.setFirstDayOfWeek(Calendar.MONDAY);
textView_result.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String getDate = textView_result.getText().toString();
String[] getDate2 = getDate.split(":");
String getDateST = getDate2[0];
Log.d(TAG, "태그 getDateST : " + getDateST);
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
AlertDialog dialog = builder.setTitle("일정을 삭제하겠습니까?")
.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 확인 클릭 시 실행할 거 작성
Date writeDate = null;
try {
writeDate = simpleDate.parse(getDateST);
Log.d(TAG, "태그 writeDate: " +writeDate);
} catch (ParseException e) {
e.printStackTrace();
}
Long currentLong = writeDate.getTime();
Log.d(TAG, "태그 currentLong : " + currentLong);
compactCalendarView.removeEvents(currentLong);
textView_result.setText("");
Toast.makeText(MainActivity.this, "일정이 삭제되었습니다.", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 취소 클릭 시 실행할 거 작성
Toast.makeText(MainActivity.this, "일정삭제가 취소되었습니다..", Toast.LENGTH_SHORT).show();
}
})
.create();
dialog.show();
}
});
// 날짜 클릭 이벤트 관련 코드
compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener() {
@Override
public void onDayClick(Date dateClicked) {
Log.d(TAG, "태그 dateClicked : " + dateClicked);
List<Event> events = compactCalendarView.getEvents(dateClicked);
// 해당날짜에 이벤트가 있으면
if (events.size() > 0) {
textView_result.setText(events.get(0).getData().toString());
}
// 해당날짜에 이벤트가 없으면
else {
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd", Locale.KOREA);
String clickDate = simpleDate.format(dateClicked);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
AlertDialog dialog = builder.setTitle("일정추가하기")
.setMessage(clickDate + "에 일정을 추가하시겠습니까?")
.setPositiveButton("확인", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 확인 클릭 시 실행할 거 작성
Toast.makeText(MainActivity.this, "확인되었습니다.", Toast.LENGTH_SHORT).show();
EditText editText = new EditText(MainActivity.this);
// 일정입력할 팝업 띄우기
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
AlertDialog dialogText = builder.setTitle("추가할 일정을 입력해 주세요.")
// .setMessage("메시지 입력")
.setView(editText)
.setPositiveButton("저장하기", new DialogInterface.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(DialogInterface dialog, int which) {
String dateClicked_st = simpleDate.format(dateClicked);
Log.d(TAG, "태그 dateClicked_st : " + dateClicked_st);
Date currentDay = null;
try {
// .parse 함수 : Parses text from a string to produce a Date (문자열에서 텍스트를 분석하여 날짜 생성)
currentDay = simpleDate.parse(dateClicked_st);
Log.d(TAG, "태그 currentDay : " + currentDay);
} catch (ParseException e) {
e.printStackTrace();
}
Long currentLong = currentDay.getTime();
Log.d(TAG, "태그 currentLong : " + currentLong);
Event ev1 = new Event(Color.GREEN, currentLong, clickDate + " : "+ editText.getText().toString());
compactCalendarView.addEvent(ev1);
textView_result.setText(clickDate + " : "+ editText.getText().toString());
Toast.makeText(MainActivity.this, "일정이 저장되었습니다.", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 취소 클릭 시 실행할 거 작성
Toast.makeText(MainActivity.this, "일정입력 취소되었습니다.", Toast.LENGTH_SHORT).show();
}
})
.create();
dialogText.show();
}
})
.setNegativeButton("취소", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 취소 클릭 시 실행할 거 작성
Toast.makeText(MainActivity.this, "취소되었습니다.", Toast.LENGTH_SHORT).show();
}
})
.create();
dialog.show();
}
}
@Override
public void onMonthScroll(Date firstDayOfNewMonth) {
textView_month.setText(dateFormatForMonth.format(firstDayOfNewMonth));
Log.d(TAG, "Month was scrolled to: " + firstDayOfNewMonth);
}
});
}
}
'안드로이드 자바' 카테고리의 다른 글
[Java][Android] 이미지 회전 (0) | 2022.03.02 |
---|---|
[Java][Android] 안드로이드 - ViewPager2 (2) | 2022.03.01 |
[JAVA][Android] 리사이클러뷰 (원리) (0) | 2022.02.26 |
[JAVA][Android] 막대 그래프 만들기 (0) | 2022.02.22 |
[JAVA][Android] 키보드 보이기/숨기기 (0) | 2022.02.20 |