본문 바로가기
App Programming/Android Studio

[Android Studio] 토스트 (Toast) 커스텀하기

by goatlab 2022. 11. 7.
728x90
반응형
SMALL

토스트 (Toast)

 

토스트 (Toast)는 사용자에게 짧은 메시지 형식으로 정보를 전달하는 팝업을 의미한다.

 

xml : drawable

 

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <corners
        android:radius="30dp"
        />
    <solid
        android:color="#A19CFF"
        />
    <padding
        android:left="0dp"
        android:top="0dp"
        android:right="0dp"
        android:bottom="0dp"
        />
    <size
        android:width="150dp"
        android:height="40dp"
        />
</shape>

 

xml : layout

 

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

    <TextView
        android:id="@+id/textboard"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:textSize="15sp"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:background="@drawable/toast_shape"/>
</LinearLayout>

 

java

 

public void customToastView(String text){

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.toast_board, (ViewGroup) findViewById(R.id.toast_layout_root));
        TextView textView = layout.findViewById(R.id.textboard);
        textView.setText(text);

        Toast toastView = Toast.makeText(getApplicationContext(),text, Toast.LENGTH_SHORT);
        toastView.setGravity(Gravity.BOTTOM,0,30);
        toastView.setView(layout);
        toastView.show();
    }

728x90
반응형
LIST