안드로이드 자바
[Java][Android] 토스트(Toast) 메시지 커스텀하기
teamnova
2024. 10. 27. 16:28
728x90
안녕하세요
이번시간에는 토스트 메시지를 커스텀하는 방법을 알아보겠습니다.
배경색, 텍스트 크기, 색상, 아이콘 등을 적용해서 커스텀 토스트 메시지를 만들고, 기존 토스트 메시지와 비교해보겠습니다.
전체 코드입니다.
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnShowToast = findViewById(R.id.btnShowToast);
Button btnShowPreToast = findViewById(R.id.btnShowPreToast);
// 기존 토스트 메시지
btnShowPreToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "기존 토스트 메시지", Toast.LENGTH_SHORT).show();
}
});
// 커스텀 토스트 메시지
btnShowToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomToast();
}
});
}
private void showCustomToast() {
// LayoutInflater를 사용하여 커스텀 토스트 레이아웃을 인플레이트
LayoutInflater inflater = getLayoutInflater();
View customToastView = inflater.inflate(R.layout.custom_toast, findViewById(R.id.custom_toast_container));
// 커스텀 텍스트뷰 설정
TextView toastText = customToastView.findViewById(R.id.toastText);
toastText.setText("커스텀 토스트 메시지 입니다~");
// 토스트 객체 생성 및 설정
Toast customToast = new Toast(getApplicationContext());
customToast.setDuration(Toast.LENGTH_SHORT);
customToast.setGravity(Gravity.BOTTOM, 0, 200);
customToast.setView(customToastView);
customToast.show();
}
}
activity_main.xml
<!-- res/layout/activity_main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
>
<Button
android:id="@+id/btnShowPreToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="200dp"
android:text="기존 토스트 메시지 보이기" />
<Button
android:id="@+id/btnShowToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:text="커스텀 토스트 메시지 보이기" />
</LinearLayout>
res/layout 폴더에 추가할 custom_toast.xml 파일입니다.
custom_toast.xml
<!-- res/layout/custom_toast.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="8dp"
android:background="@drawable/toast_background">
<ImageView
android:id="@+id/toastIcon"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@android:color/holo_red_light"
android:layout_marginEnd="8dp"/>
<TextView
android:id="@+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="16sp"
android:text="커스텀 토스트 메시지 입력"/>
</LinearLayout>
res/drawble 폴더에 toast_background.xml 파일을 생성해 배경을 설정합니다.
toast_background.xml
<!-- res/drawable/toast_background.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#333333"/>
<corners android:radius="10dp"/>
</shape>
이처럼 커스텀 토스트 메시지는, 별도의 레이아웃 파일을 res/layout 폴더에 만들고 거기에 원하는 디자인을 적용해서 사용합니다.
MainActivity.java 에서는, 이 레이아웃 파일을 인플레이트하여 토스트에 연결고 보여주기만 하면 됩니다.
시연영상입니다.