Programming/Mobile

[안드로이드] 기본.2 Toast 메시지 띄우기

tavris 2017. 5. 23. 23:23

기본편.2 Toast 메시지 띄우기


이번 편에서는 지난번에 이어 토스트 메시지의 고급 사용법을 알아보고자 합니다.


기본 토스트 메시지도 수정이 가능하다는 것을 아시나요.

일반적으로 어디를 검색해도 모두 동일하게 한가지 사용법 밖에 없다는 것이 많은 사람들을 헷갈리게 하는 부분입니다.

1
Toast.makeText(Context, String, Int).show();


바로 위 사용법인데요. 토스트 메시지의 특성상 간략하게 쓰이기에 대부분 위처럼 사용합니다.

하지만, 기본 토스트 메시지도 좀더 다양한 설정을 할 수 있는 방법이 있으니, 제공되는 Public Method를 사용하는 방법이죠.


자. 아래 목록은 Google API에서 제공하는 Public Method입니다.

Public methods
voidcancel()

Close the view if it's showing, or don't show it if it isn't showing yet.

intgetDuration()

Return the duration.

intgetGravity()

Get the location at which the notification should appear on the screen.

floatgetHorizontalMargin()

Return the horizontal margin.

floatgetVerticalMargin()

Return the vertical margin.

ViewgetView()

Return the view.

intgetXOffset()

Return the X offset in pixels to apply to the gravity's location.

intgetYOffset()

Return the Y offset in pixels to apply to the gravity's location.

static ToastmakeText(Context context, int resId, int duration)

Make a standard toast that just contains a text view with the text from a resource.

static ToastmakeText(Context context, CharSequence text, int duration)

Make a standard toast that just contains a text view.

voidsetDuration(int duration)

Set how long to show the view for.

voidsetGravity(int gravity, int xOffset, int yOffset)

Set the location at which the notification should appear on the screen.

voidsetMargin(float horizontalMargin, float verticalMargin)

Set the margins of the view.

voidsetText(int resId)

Update the text in a Toast that was previously created using one of the makeText() methods.

voidsetText(CharSequence s)

Update the text in a Toast that was previously created using one of the makeText() methods.

voidsetView(View view)

Set the view to show.

voidshow()

Show the view for the specified duration.


기본적으로 show()와 mackeText()는 많이 보셨으니까 아실거라고 생각합니다.

여기서 기본적으로 Method 이름에 붙은 것중 Get과 Set의 차이점을 먼저 알아보도록 하겠습니다.


Get*******()으로 되어있는 함수들은 현재 적용되어있는 정보를 가져오는 함수들입니다.

반면, Set*********()으로 되어있는 함수들은 입력하는 값으로 현재 정보를 변경하는 함수들이 되겠습니다.


그럼, 저희는 수정 부분이니 Get*****() Method가 아닌 Set******() Method가 주사용 함수들이 되겠습니다.

자, 먼저 사용하고자하는 함수들을 하나씩 보겠습니다.


SetDuration(int duration)

 토스트 메시지가 얼마나 오랬동안 화면에 보여질지를 설정합니다.

(사실 이 함수는 초기 선언에 포함되어있어 많이 사용하지 않습니다. 바로 makeToast의 3번째 인자이기 때문입니다.)

 SetGravity(int, int, int)

 토스트 메시지가 표시되는 화면의 위치를 설정합니다.

(첫번째 인자로, GRAVITY.XXXX, 두번째 인자로 X-위치값, 세번째 인자로 Y-위치값을 입력하면 됩니다.)

SetMargin(float, float) 

토스트 메시지의 여백을 설정합니다.

(사실. 해당 함수는 거의 사용을 안합니다. SetGravity Method 중 X, Y 값을 설정한다고 보시면됩니다.)

 SetView(View)

토스트 메시지의 배경을 설정합니다.

(토스트 메시지의 배경 View를 설정하는 기능입니다.) 


기본 토스트 메시지 설정에서 많이 사용되는 부분은 SetGravity Method입니다.

위치를 자유자재로 변경할 수 있기 때문이죠. 또한 Custom Toast를 만들때는 SetView Method를 사용합니다.

(Custom Toast는 다음편에 연재하도록 하겠습니다.)


그럼 마지막으로 자주 사용되는 기본 Toast 메시지의 사용방법입니다.


1
Toast.makeText(Context, String, Int).SetGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0150).show();


위의 방식으로 토스트 메시지를 생성한다면, 하단에서 150px만큼 세로로 여백을 준 가로중앙정렬 토스트 메시지가 표시됩니다.



뭔가, 심화편이라고해서 어려운 것을 알려드리고자 했으나... 오히려.. 더 쉬워진듯한 느낌이 들게되는편입니다.

다음에는 Custom 토스트 메시지를 발생시키는 것을 알려드리도록 하겠습니다.