안드로이드 자바

[JAVA][Android]Toast 메시지 위치 변경해 보여주기(2)

teamnova 2024. 11. 5. 12:00
728x90

오늘은 액티비티에서 Android API 30 이후에서도 동작 되는 Toast 메시지 보여지는 위치를 변경하는 예시를 만들어 보겠습니다.

 

 

 

액티비티용 레이아웃 xml 파일 코드 (activity_main.xml)

<?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:orientation="vertical"
    android:padding="16dp">

    <Button
        android:id="@+id/btnTopLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="좌측 상단" />

    <Button
        android:id="@+id/btnCenter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="중앙" />

    <Button
        android:id="@+id/btnBottomRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="우측 하단" />

</LinearLayout>



 

Toast용 레이아웃 xml 파일 코드 (custom_toast.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_toast_container"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#CC000000"
    android:padding="16dp">

    <TextView
        android:id="@+id/toast_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFFFFF"/>
</LinearLayout>

 

 

 

액티비티 자바 코드

public class MainActivity extends AppCompatActivity {

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

        Button btnTopLeft = findViewById(R.id.btnTopLeft); //좌측 상단 Toast 메시지 표시용 버튼
        Button btnCenter = findViewById(R.id.btnCenter); //중앙 Toast 메시지 표시용 버튼
        Button btnBottomRight = findViewById(R.id.btnBottomRight); //우측 하단 Toast 메시지 표시용 버튼

        //좌측 상단 Toast 메시지 표시용 버튼 이벤트 설정
        btnTopLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast 메시지 좌측 상단 보여주기
                showCustomToast("좌측 상단 Toast", Gravity.TOP | Gravity.START, 0, 0);
            }
        });

        //중앙 Toast 메시지 표시용 버튼 이벤트 설정
        btnCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast 메시지 중앙 보여주기
                showCustomToast("중앙 Toast", Gravity.CENTER, 0, 0);
            }
        });

        //우측 하단 Toast 메시지 표시용 버튼 이벤트 설정
        btnBottomRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast 메시지 우측 하단 보여주기
                showCustomToast("우측 하단 Toast", Gravity.BOTTOM | Gravity.END, 0, 0);
            }
        });
    }

    
    //Toast 메시지 생성 및 표시 메서드
    private void showCustomToast(String message, int gravity, int xOffset, int yOffset) {
        
        // 보여줄 Toast 메시지에 적용할 레이아웃 inflate 및 해당 레이아웃 객체 가져오기
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast, null);

        // 레이아웃의 텍스트 뷰에 보여줄 메시지 설정
        TextView text = layout.findViewById(R.id.toast_text);
        text.setText(message);

        //Toast 객체 생성
        Toast toast = new Toast(getApplicationContext());
        
        //Toast 메시지가 나타날 위치 설정
        toast.setGravity(gravity, xOffset, yOffset);

        //Toast 메시지가 표시될 기간 설정
        toast.setDuration(Toast.LENGTH_SHORT);
        
        //Toast를 통해 보여줄 레이아웃 설정
        toast.setView(layout);

        //Toast 메시지 보여주기
        toast.show();
    }
}




 

 

 

 

 

 

 

 

실행 영상

 

Toast 메시지 보이는 위치가 기본위치가 아닌 설정한 위치로 변경된 것을 확인할 수 있습니다.