티스토리 뷰

간단한 메시지를 보여줄 때, Toast 대신 Snackbar를 사용하는 경우도 있다. Snackbar는 Android내장 라이브러리가 아닌,외부 라이브러리이므로 Snackbar가 들어있는 Material 라이브러리를 받아야한다.

 

사용 방법은 Toast와 매우 유사하니 간단하게 화면에 띄워보는 예제만 해보자.


▶Snackbar 띄우기

 

◎activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="415dp"
            android:layout_height="61dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="596dp" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" app:layout_constraintHorizontal_bias="0.0"
            android:id="@+id/linearLayout">
        <EditText
                android:layout_width="115dp"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:hint="X 위치"
                android:ems="10"
                android:textSize="20sp"
                android:id="@+id/editText_x" android:layout_weight="1"/>
        <EditText
                android:layout_width="117dp"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:hint="X 위치"
                android:ems="10"
                android:textSize="20sp"
                android:id="@+id/editText_y" android:layout_weight="1"/>
        <Button
                android:text="띄우기"
                android:textSize="20sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:id="@+id/button" android:layout_weight="1"/>
    </LinearLayout>
    <Button
            android:text="Snackbar"
            android:layout_width="128dp"
            android:layout_height="41dp" android:id="@+id/button2"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" android:layout_marginTop="8dp"
            app:layout_constraintHorizontal_bias="1.0" app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintVertical_bias="0.0"/>
</androidx.constraintlayout.widget.ConstraintLayout>

 

◎ActivityMain.java

package com.example.toast_ex;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity {
    EditText editText;
    EditText editText2;


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

        editText = findViewById(R.id.editText_x);
        editText2 = findViewById(R.id.editText_y);

        Button button = findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(
                        R.layout.toastborder,
                        (ViewGroup) findViewById(R.id.toast_layout_root)
                );

                TextView text = layout.findViewById(R.id.text1);
                text.setText("모양 바뀐 토스트");

                Toast toast = new Toast(getApplicationContext());

                int xOffset = Integer.parseInt(editText.getText().toString());
                int yOffset = Integer.parseInt(editText2.getText().toString());

                toast.setGravity(Gravity.TOP, xOffset, yOffset);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
            }
        });

        Button button2 = findViewById(R.id.button2);

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "오레오 팝니다.", Snackbar.LENGTH_LONG).show();
            }
        });
    }
}

반응형
Comments