-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomToast.java
58 lines (48 loc) · 1.88 KB
/
CustomToast.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Layout custom_toast.xml
/*
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@drawable/toast_background">
<ImageView
android:id="@+id/toast_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_check_circle" />
<TextView
android:id="@+id/toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:text="Custom Toast Message"
android:textColor="#FFFFFF"
android:textSize="16sp" />
</LinearLayout>
*/
//toast_background.xml (drawable)
/* <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#333333"/>
<corners android:radius="10dp"/>
<padding android:left="10dp" android:top="5dp" android:right="10dp" android:bottom="5dp"/>
</shape> */
// Custom Toast method in Java
private void customToast() {
// Inflate the custom toast layout
LayoutInflater inflater = getLayoutInflater();
View customView = inflater.inflate(R.layout.custom_toast, null);
// Find and set the text in the custom layout
TextView toastTitle = customView.findViewById(R.id.toast_text);
toastTitle.setText("Course Enrolled");
// Create a new Toast and set its properties
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(customView);
// Set the position of the Toast on the screen
toast.setGravity(Gravity.BOTTOM, 0, 80); // 80 is the offset from the bottom
// Show the custom Toast
toast.show();
}