본문 바로가기
안드로이드 자바

[Android][Java] 토스트 메시지 꾸미기

by teamnova 2023. 1. 9.

안녕하세요.

오늘은 

https://github.com/Spikeysanju/MotionToast

 

GitHub - Spikeysanju/MotionToast: 🌈 A Beautiful Motion Toast Library for Kotlin Android

🌈 A Beautiful Motion Toast Library for Kotlin Android - GitHub - Spikeysanju/MotionToast: 🌈 A Beautiful Motion Toast Library for Kotlin Android

github.com

위 링크의 라이브러리를 사용해서 토스트 메시지를 꾸미는 방법에 대해 알아보도록 하겠습니다.

 

먼저 gradle에 라이브러리를 등록해줍니다.

 

build.gradle(Module:프로젝트명:app)
dependencies 괄호 안에 아래 코드를 넣어주시면 됩니다.

implementation 'com.github.Spikeysanju:MotionToast:1.4'

 

다음으로 레이아웃(xml 파일) 입니다.

 

motion_toast.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="50dp"
        android:text="메시지 타입"
        android:textSize="25sp"
        android:textStyle="bold"/>

    <RadioGroup
        android:id="@+id/toast_type"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Color Motion"
            android:id="@+id/color_motion"
            android:checked="true"
            />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Dark"
            android:id="@+id/dark"/>
    </RadioGroup>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:text="메시지 스타일"
        android:textSize="25sp"
        android:textStyle="bold"/>

    <RadioGroup
        android:id="@+id/toast_style"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/success"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Success"/>

        <RadioButton
            android:id="@+id/error"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Error"/>

        <RadioButton
            android:id="@+id/warning"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="warning"/>

        <RadioButton
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Info"/>
    </RadioGroup>

    <Button
        android:id="@+id/create_toast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:layout_gravity="center"
        android:text="토스트 생성"/>
</LinearLayout>

 

메시지 타입을 선택할 수 있는 라디오그룹,

어떤 스타일의 메시지를 띄울지 선택할 수 있는 라디오그룹, 

그리고 토스트 메시지를 띄우기 위한 버튼으로 구성된 레이아웃 파일입니다.

 

다음으로 java 파일입니다.

 

MotionToastActivity.java

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;

import www.sanju.motiontoast.MotionToast;
import www.sanju.motiontoast.MotionToastStyle;

public class MotionToastActivity extends AppCompatActivity {

    String gToastType;

    MotionToastStyle gMotionToastStyle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.motion_toast);

        //기본값으로 초기화
        gToastType = "motion";

        gMotionToastStyle = MotionToastStyle.INFO;

        //메시지 타입 선택
        RadioGroup toastType = findViewById(R.id.toast_type);
        toastType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {

                if(checkId == R.id.color_motion){
                    gToastType = "colorMotion";

                }else if(checkId == R.id.dark){
                    gToastType = "dark";

                }
            }
        });

        //메시지 스타일 선택
        RadioGroup toastStyle = findViewById(R.id.toast_style);
        toastStyle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {

                if(checkId == R.id.info){
                    gMotionToastStyle = MotionToastStyle.INFO;

                }else if(checkId == R.id.success){
                    gMotionToastStyle = MotionToastStyle.SUCCESS;

                }else if(checkId == R.id.warning){
                    gMotionToastStyle = MotionToastStyle.WARNING;

                }else if(checkId == R.id.error){
                    gMotionToastStyle = MotionToastStyle.ERROR;
                }
            }
        });

        //메시지 실행버튼 이벤트
        Button createToast = findViewById(R.id.create_toast);
        createToast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String title = "title"; // 토스트 메시지의 제목
                String message = "message"; // 토스트 메시지의 내용

                int position = MotionToast.GRAVITY_BOTTOM; // GRAVITY_TOP,GRAVITY_CENTER
                long duration = MotionToast.SHORT_DURATION; // LONG_DURATION

                //메시지 생성
                createToast(title, message, gMotionToastStyle, position, duration);
            }
        });


    }//onCreate

    /**
     * 메시지 실행
     * @param title 제목
     * @param message 내용
     * @param style 스타일
     * @param position 위치
     * @param duration 구동시간
     */
    private void createToast(String title, String message, MotionToastStyle style,
                             int position, long duration) {

        if(gToastType.equals("colorMotion")){
            MotionToast.Companion.createColorToast(MotionToastActivity.this,
                    title,
                    message,
                    style,
                    position,
                    duration,
                    ResourcesCompat.getFont(getApplicationContext(), R.font.helvetica_regular));

        }else if(gToastType.equals("dark")){
            MotionToast.Companion.darkToast(MotionToastActivity.this,
                    title,
                    message,
                    style,
                    position,
                    duration,
                    ResourcesCompat.getFont(getApplicationContext(), R.font.helvetica_regular));

        }
    }


}

 

선택된 라디오 버튼에 따라 다른 유형의 토스트 메시지를 띄워줄 수 있는 코드입니다.

 

실행화면 입니다.

 

 

오늘 준비한 내용은 여기까지 입니다.

궁금한 점은 댓글로 남겨주세요.

감사합니다!