반응형

이번시간엔 안드로이드를 이용한 달력 만드는 방법을 포스팅 해보려고한다. 솔직히 구글, 네이버쪽을 쭉 보면서 많은 도움을 받았다. 근데 내가 찾았던 블로그 명이 기억나지않아서 출저가 어딘지에 대해 알리지 못하는 거에 대해선 양해를 구하고 싶다.


1. activity_main.xml Text 설정

API 23으로 설정해주면 대강 이런식으로 나온다. 달력은 지니모션 돌려보면 다 나오니깐 너무 신경쓰지 않는다. 내가 사용한 코드는 아래와 같다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:background="#FFFFFFFF"
>

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10px"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
>

<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="PRE"
android:id="@+id/pre"
android:layout_weight="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="today"
android:id="@+id/today"
android:layout_weight="3"
android:textColor="#FF000000"
android:gravity="center_horizontal|center_vertical"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="NEXT"
android:id="@+id/next"
android:layout_weight="1"
/>

</LinearLayout>

<TableLayout
android:id="@+id/week"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="true"
android:stretchColumns="true"
android:background="#FF8DC701"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="일"
android:textColor="#FFFF0000"

android:gravity="center_horizontal" />
<TextView
android:text="월"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:textColor="#FF000000"
android:layout_height="wrap_content"/>
<TextView
android:text="화"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:textColor="#FF000000"
android:layout_height="wrap_content"/>
<TextView
android:text="수"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:textColor="#FF000000"
android:layout_height="wrap_content"/>
<TextView
android:text="목"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:textColor="#FF000000"
android:layout_height="wrap_content"/>
<TextView
android:text="금"
android:gravity="center_horizontal"
android:layout_width="wrap_content"
android:textColor="#FF000000"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="토"
android:textColor="#FF0000FF"
android:gravity="center_horizontal"/>
</TableRow>
</TableLayout>

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/table"
android:background="#FFF6F3A4"
android:layout_marginLeft="10px"
android:layout_marginRight="10px"
/>
</LinearLayout>

달력이다 보니 좀 긴감이 없지않아 있지만 이정도가 딱 맞는거 같다. 색깔이나 이런부분은 내 컴퓨터 사양과 맞지 않는 부분이 많아서 내가 색깔을 이것저것 입혀봐서 괜찮다 싶은 거로 설정했다. 이 코드 복사해서 쓰는거는 상관없지만 색깔맞추려면 본인 컴퓨터 사양에 맞게 설정해야 될것이다.(맥북기준)



2. Main_Activity.java 코드 

package com.example.gunjoolee.calendar;

import java.util.Date;
import java.util.Vector;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
/** Called when the activity is first created. */

Vector vec;
int firstDay;

int totDays;
int iYear;
int iMonth;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Date today = new Date(); ->오늘날짜
iYear = today.getYear(); ->년도
iMonth = today.getMonth(); ->달 수

vec = new Vector();
TableLayout table = (TableLayout)findViewById( R.id.table );
for( int i = 0 ; i < 6 ; i++ ){
TableRow tr = new TableRow( this );
for( int j = 0 ; j < 7 ; j++ ){
TextView tv = new TextView( this );

if( j == 0 )
tv.setTextColor( Color.RED );
else if( j == 6 )
tv.setTextColor( Color.BLUE ); -> 테이블레이아웃쪽 달력설정

else
tv.setTextColor( Color.BLACK );

tv.setGravity( Gravity.CENTER_HORIZONTAL );

tr.addView( tv );
vec.add( tv );
}
table.addView( tr );
}

table.setStretchAllColumns( true );

table = (TableLayout)findViewById( R.id.week );
table.setStretchAllColumns( true );

setCalendar( iYear, iMonth );

Button btn = (Button)findViewById( R.id.pre ); -> pre 버튼 설정
btn.setOnClickListener( new Button.OnClickListener(){

public void onClick(View v) {

iMonth--;
setCalendar( iYear, iMonth );

}
});

btn = (Button)findViewById( R.id.next ); ->next 버튼 설정
btn.setOnClickListener( new Button.OnClickListener(){

public void onClick(View v) {

iMonth++;
setCalendar( iYear, iMonth );

}
});
}

private void setCalendar( int year, int month ){


Date date = new Date();
date.setYear( year );
date.setMonth( month );
date.setDate( 1 );
firstDay = date.getDay();

totDays = 31;
for( int i = 29 ; i <= 32 ; i++ ){
date.setDate( i );
if( date.getDate() == 1 ){ ->달이 30일인지, 31일인지 설정
totDays = i - 1;
break;
}
}

Log.i("mylog", firstDay + " " + totDays );
date = new Date();
date.setYear( year );
date.setMonth( month );

iYear = date.getYear();
iMonth = date.getMonth();

TextView tvToday = (TextView)findViewById( R.id.today ); ->년,월,날짜설정
tvToday.setText( (iYear + 1900) + "년" + (iMonth + 1) + "월");



for( int i = 0 ; i < vec.size() ; i++ ){
((TextView)vec.get( i )).setText("");
}

int iDate = 1;
for( int i = firstDay ; i < firstDay + totDays; i++ ){

((TextView)vec.get( i )).setText( String.valueOf( iDate++ ) );
}

}
}

달력 java코드이다. 구글이나 네이버쪽에서 참고한 코드인데 출저가 어디였는지 기록해놓지 못해서 아쉽다. 아무튼 이런식으로 짜게되면 만들고자하는 대략의 코드가 나온다. 책에도 많이 나와있고 인터넷에도 많이 도는 코드니까 외우려고 하지말았으면 좋겠다. 


3. 실행

저렇게 디자인을 다하고 저걸 그대로 실행시켜보면 다음과 같은 결과가 얻어졌다. 


결과가 좀 작게나와서 아쉽지만 PRE, NEXT버튼 누르면 아주 잘넘어갈것이다. 만약 이번년도 달력만 만들고 싶으면 자바코드에서 break문 걸고 코드 수정하면 이번년도 달력만 만들 수 있다.

반응형
블로그 이미지

만년필석사

,