-
Notifications
You must be signed in to change notification settings - Fork 810
/
Copy pathMainActivity.java
106 lines (99 loc) · 4.61 KB
/
MainActivity.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package es.dmoral.toastysample;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;
import android.view.View;
import es.dmoral.toasty.Toasty;
import static android.graphics.Typeface.BOLD_ITALIC;
/**
* This file is part of Toasty.
*
* Toasty is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Toasty is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Toasty. If not, see <http://www.gnu.org/licenses/>.
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button_error_toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.error(MainActivity.this, R.string.error_message, Toasty.LENGTH_SHORT, true).show();
}
});
findViewById(R.id.button_success_toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.success(MainActivity.this, R.string.success_message, Toasty.LENGTH_SHORT, true).show();
}
});
findViewById(R.id.button_info_toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.info(MainActivity.this, R.string.info_message, Toasty.LENGTH_SHORT, true).show();
}
});
findViewById(R.id.button_warning_toast).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.warning(MainActivity.this, R.string.warning_message, Toasty.LENGTH_SHORT, true).show();
}
});
findViewById(R.id.button_normal_toast_wo_icon).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.normal(MainActivity.this, R.string.normal_message_without_icon).show();
}
});
findViewById(R.id.button_normal_toast_w_icon).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Drawable icon = getResources().getDrawable(R.drawable.ic_pets_white_48dp);
Toasty.normal(MainActivity.this, R.string.normal_message_with_icon, icon).show();
}
});
findViewById(R.id.button_info_toast_with_formatting).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.info(MainActivity.this, getFormattedMessage()).show();
}
});
findViewById(R.id.button_custom_config).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toasty.Config.getInstance()
.setToastTypeface(Typeface.createFromAsset(getAssets(), "PCap Terminal.otf"))
.allowQueue(false)
.apply();
Toasty.custom(MainActivity.this, R.string.custom_message, getResources().getDrawable(R.drawable.laptop512),
android.R.color.black, android.R.color.holo_green_light, Toasty.LENGTH_SHORT, true, true).show();
Toasty.Config.reset(); // Use this if you want to use the configuration above only once
}
});
}
private CharSequence getFormattedMessage() {
final String prefix = "Formatted ";
final String highlight = "bold italic";
final String suffix = " text";
SpannableStringBuilder ssb = new SpannableStringBuilder(prefix).append(highlight).append(suffix);
int prefixLen = prefix.length();
ssb.setSpan(new StyleSpan(BOLD_ITALIC),
prefixLen, prefixLen + highlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return ssb;
}
}