2015년 10월 18일 일요일

안드로이드에서 현재 시간을 24시간제로 표시법(각종 시간 표시법)





현재 시간을 24시간제로 표시하고자 한다면 android.text.format.DateFormat라는 클래스를 이요하면 된다.
이 클래스가 제공하는 메소드 중에서 다음 메소드가 이 기능을 제공한다.

public static CharSequence format (CharSequence inFormat, long  inTimeInMillis)

첫 번째 매개인자 inFormat에 사용되는 형식은 아래 표의 규칙대로 하면된다
(http://developer.android.com/reference/java/text/SimpleDateFormat.html 참조)

SymbolMeaningKindExample
Dday in year(Number)189
Eday of week(Text)E/EE/EEE:Tue, EEEE:Tuesday, EEEEE:T
Fday of week in month(Number)2 (2nd Wed in July)
Gera designator(Text)AD
Hhour in day (0-23)(Number)0
Khour in am/pm (0-11)(Number)0
Lstand-alone month(Text)L:1 LL:01 LLL:Jan LLLL:January LLLLL:J
Mmonth in year(Text)M:1 MM:01 MMM:Jan MMMM:January MMMMM:J
Sfractional seconds(Number)978
Wweek in month(Number)2
Ztime zone (RFC 822)(Time Zone)Z/ZZ/ZZZ:-0800 ZZZZ:GMT-08:00 ZZZZZ:-08:00
aam/pm marker(Text)PM
cstand-alone day of week(Text)c/cc/ccc:Tue, cccc:Tuesday, ccccc:T
dday in month(Number)10
hhour in am/pm (1-12)(Number)12
khour in day (1-24)(Number)24
mminute in hour(Number)30
ssecond in minute(Number)55
wweek in year(Number)27
yyear(Number)yy:10 y/yyy/yyyy:2010
ztime zone(Time Zone)z/zz/zzz:PST zzzz:Pacific Standard Time
'escape for text(Delimiter)'Date=':Date=
''single quote(Literal)'o''clock':o'clock
예를들면

MM : 11, 10과 같이 월 표시를 두 자리 숫자로 표현
MMM : Nov, Oct...와 같이 월 표시를 영문 약어로. 
          한글 폰에서는 그냥 '월'이라는 글자로 표시

MMMM : November, October...와 같이 월 표시를 영문 full name으로 표시

yyyy-MM-dd h:mm => 2013-11-25 3:25

yyyy-MM-dd h:mm a => 2013-11-25 3:25 pm

yyyy-MM-dd k:mm => 2013-11-25 15:25 (24시간제로 표시)


예제 소스는...

String crrTime = System.currentTimeMillis().toString();
String now = DateFormat.format("yyyy MMM dd k:mm", crrTime);
        
TextView txt = (TextView)findViewById(R.id.txt);
txt.setText(now);

여기서 DateFormat은 자바의 DateFormat(
java.text.DateFormat)이 아니라 안드로이드의 DateFormat이다. 
클래스 이름이 동일한 DateFormat은 자바에도 있고 안드로이드에도 있다.

android.text.format.DateFormat;

2015년 10월 15일 목요일

JON DUCKETT의 「HTML & CSS」라는 자료를 소개합니다.





요즘 틈틈이 읽고 있는 책인데 상당히 괜찮은 것 같아서 소개합니다.
단순히 HTML과 CSS의 기능만 소개하는 정도가 아니라 보다 근원적인 뒷 배경 
이야기들과 각종 노하우들과 팁들이 소개되고 있는 책입니다.

책의 편집 형태도 장황한 산문식 설명이 아니라 보기 쉽고 가독성있게 핵심적인 설명으로 유익하게 보고 있는 책입니다.
단지 영문 자료라는 약간의 무거움은 있겠네요.


Jon Duckett, 「HTML & CSS - Design and Build Websites 」, John Wiley & Sons, Inc.

아래 사이트에 관련 소스들과 더 많은 자료들이 있습니다.


PDF 자료는 검색해 보면 나올 겁니다^^

HTML의 th와 td의 개념





HTML의 th와 td의 개념

<th>내용</th> : table head 약자로, 표의 제목을 쓰는 역할. bold체로 표시됨
<tr>내용</tr> : table row 약자로 가로줄을 만드는 역할
<td>내용</td> : table data 약자로 셀을 만드는 역할

<table border="1" width="350" >
   <tr align="center" >
        <th>Number</th>
        <th>Name</th>
   </tr>
   <tr align="center">
        <td>111</td>
        <td>Kim. S. Y</td>
   </tr>
   <tr align="center">
        <td>222</td>
        <td>Lee. K. H</td>
   </tr>
</table>

결과는 아래와 같다.



Android TextView에서 웹 페이지로 링크(link) 거는 법





TextView txt = (TextView)findViewById(R.id.txt);

txt.setLinksClickable(true);
txt.setMovementMethod(LinkMovementMethod.getInstance());

String urlNaver = "<a href='http://www.naver.com'>네 이 버</a><br>";
String urlGoogle = "<a href='http://www.google.com'>구 글</a>";

txt.setText(Html.fromHtml(urlNaver));

txt.append(Html.fromHtml(urlGoogle));

안드로이드 SQLITE에 대해서 주의해야 할 사항-ONCREATE, ONUPGRADE 호출 시점





안드로이드 SQLite에 대해서 주의해야 할 사항

SQLiteOpenHelp 클래스의 두 abastract 메소드인 onCreate()와 onUpgrade()가 호출되는 시점에 대해서 주의해야 할, 그리고 알아야 할 사항이 있다.

여기서 MyDB는 SQLiteOpenHelper 클래스를 상속받은 클래스라고 할 경우
(class MyDB extends SQLiteOpenHelper)

① MyDB dbHelp = new MyDB(this);
  ⇒ 이 단계에서는 SQLiteOpenHelper를 상속 받은 MyDB의 생성자만 호출이 
      되고 MyDB의 onCreate()는 아직 호출이 안된다.

      SQLiteDatabase db = dbHelp.getReadableDatabase() 혹은   
      dbHelp.getWritableDatabase()가 호출 될 때 비로소
      MyDB의 onCreate()가 호출되서 테이블이 생성이 된다. 

      getReadableDatabase()하는 시점에서 테이블이 없으면
      테이블이 새롭게 생성이 되고 기존 존재하면 그것을 Radable이나 Writable 중 
      하나로 open한다.

② MyDB의 onUpgrade()가 호출되는 시점
  ⇒ super(context,"Test.db", null, 2); 의 맨 마지막 인자인 DB의 
      version 값이 상향 조정될 때 호출된다.

THREAD 안에서 THREAD를 사용할 경우 오류 문제 해법





아래와 같은 에러를 만나면 원인은 쓰레드 안에 쓰레드를 사용하였기 때문에
오류가 발생하였다.

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

해결 방법은 쓰레드 안에 Handler를 새로 선언하여 사용하면 된다.

View.OnClickListener clickSomeThing = new View.OnClickListener(){
@Override
public void onClick(View v){
Handler mHandler = new Handler(Looper.getMainLooper());

mHandler.postDelayed(new Runnable(){
@Override
public void run() {
doSomeMethod(); //쓰레드 안에서 실행되는 쓰레드
}, 0);
}
};

앱의 설치 위치 지정하기(내장 메모리, 외장 메모리)





앱의 설치 위치 지정하기(내장 메모리, 외장 메모리)

AndroidManifest.xml 에 아래와 같이 installLocation 을 추가 한다.

    package="com.myapps.test"
    android:versionCode="3"
    android:versionName="1.6"
    android:installLocation="preferExternal" >

auto : 자동으로 내장 메모리에 우선 설치 하나 공간이 없는 경우 외장 메모리에 설치.
internalOnly : 내장 메모리에 설치.
preferExternal : 외장 메모리에 설치를 우선으로 하나 공간이 없는 경우 내장 메모리에 설치.

2013년 9월 30일 월요일

RectF와 Path에 대한 개념 및 그리기 예





1) RectF의 생성자

public RectF (float left, float top, float right, float bottom)
⇒ Create a new rectangle with the specified coordinates. 
Note: no range checking is performed, so the caller must ensure that left <= right and top <= bottom.
주어진 좌표 값을 이용해서 새로운 사각형을 만드는 클래스이다. 주의할 점은 left 값이 right 값보다 작아야 되는데 이러한 값의 범위가 올바른지는 체크해야 한다는 이야기가 있고요.

Parameters
left : The X coordinate of the left side of the rectangle
top : The Y coordinate of the top of the rectangle
right : The X coordinate of the right side of the rectangle
bottom : The Y coordinate of the bottom of the rectangle

⇒ 사각형 그리는 클래스

2) Path의 메소드
public void arcTo (RectF oval, float startAngle, float sweepAngle)
⇒ Append the specified arc to the path as a new contour. If the start of the path is different from the path's current last point, then an automatic lineTo() is added to connect the current contour to the start of the arc. However, if the path is empty, then we call moveTo() with the first point of the arc.
원호를 그릴 경로(path)를 미리 지정해 놓는 메소드이다. 

Parameters
oval : The bounds of oval defining shape and size of the arc
       : 원호를 그릴 영역, 원호의 크기를 결정하는 경계선 역할
startAngle : Starting angle (in degrees) where the arc begins
                : 원호가 시작되는 지점. 각도로 표시
sweepAngle : Sweep angle (in degrees) measured clockwise
                  : 시계방향으로 원호를 그리는 각도 지정

⇒ 그림을 그릴 경로를 미리 지정. Canvas 클래스의 drawPath()를 이용해서 Path로 지정된 경로를 따라 그리기를 한다.

3) Canvas의 메소드 
public void drawPath (Path path, Paint paint)
⇒ Draw the specified path using the specified paint. The path will be filled or framed based on the Style in the paint. 앞에서 설정된 경로(path)대로 그림을 실제 그리는 역할

Parameters
path : The path to be drawn
paint : The paint used to draw the path


예제)
RectF rf = new RectF();

//좌에서 우로 50만큼 이동된 x축 위치와 위에서 아래쪽으로           
//50만큼 이동된 y축 위치와 좌에서 우로 100만큼 이동된 
//x축 위치와 위에서 아래쪽으로 100만큼 이동된 y축 위치를 잇는 
//사각형. 이 영역이 그림이 그려질 영역이다.
rf.set(50, 50, 100, 100);                      

Path path = new Path();

//그림을 그릴 path를 지정해 두고 나중에 drawPath()로 이 
//path를 따라 원호를 그린다.
//원호의 크기는 두 번째, 세 번째 인자가 지정하는 크기만큼 그린다.
//rf는 그림을 그릴 영역이고, 0은 그림을 시작할 위치인데 각도로 
//표시한다. 시계로 3시 지점이 0도이고
//6시 지점이 90도이고 9시 지점이 180도이고 12시 지점이 270도
//이다. 방향은 시계 방향으로 돌아가는 방식으로 각도를 지정한다.
path.arcTo(rf, 0, 180); 
                                 
public void onDraw(Canvas canvas) 
{
   Paint paint = new Paint();
   paint.setFlags(Paint.ANTI_ALIAS_FALG); 
   paint.setStrokeWidth(2); //선의 굵기를 2로 지정

   RectF rf = new RectF();
   rf.set(50, 50, 100, 100);

   Path path = new Path();

   //3시 지점에서 시작해서 시계 방향으로 9시 지점까지 
   //원호를 그린다.
   path.arcTo(rf, 0, 180); 
                                     
   canvas.drawPath(path, paint);
}