이번 시간에는 Toast 메시지를 쉽게 꾸미는
방법을 알아보겠습니다.
1. 실행 화면
2. 라이브러리 등록
3. 메인 화면 구성 activity_main.xml
4. 메인 코드 구현 MainActivity.java
build.gradle(Module:프로젝트명:app)
dependencies 괄호 안에 아래 코드를 넣어주시면 됩니다.
implementation 'com.github.Spikeysanju:MotionToast:1.4'
참조 문서
https://github.com/Spikeysanju/MotionToast
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="20dp"
android:layout_marginTop="50dp"
android:text="메시지 타입"
android:textSize="25sp"
android:textStyle="bold"/>
<RadioGroup
android:id="@+id/toast_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Motion"
android:id="@+id/motion"
android:checked="true"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Color Motion"
android:id="@+id/color_motion"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dark"
android:id="@+id/dark"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dark Color"
android:id="@+id/dark_color"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginLeft="20dp"
android:layout_marginTop="30dp"
android:text="메시지 스타일"
android:textSize="25sp"
android:textStyle="bold"/>
<RadioGroup
android:id="@+id/toast_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/success"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Success"/>
<RadioButton
android:id="@+id/error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Error"/>
<RadioButton
android:id="@+id/warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="warning"/>
<RadioButton
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Info"/>
</RadioGroup>
<Button
android:id="@+id/create_toast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:text="토스트 생성"/>
</LinearLayout>
public class MainActivity extends AppCompatActivity {
String gToastType;
MotionToastStyle gMotionToastStyle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//기본값으로 초기화
gToastType = "motion";
gMotionToastStyle = MotionToastStyle.INFO;
//메시지 타입 선택
RadioGroup toastType = findViewById(R.id.toast_type);
toastType.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
if(checkId == R.id.motion){
gToastType = "motion";
}else if(checkId == R.id.color_motion){
gToastType = "colorMotion";
}else if(checkId == R.id.dark){
gToastType = "dark";
}else if(checkId == R.id.dark_color){
gToastType = "darkColor";
}
}
});
//메시지 스타일 선택
RadioGroup toastStyle = findViewById(R.id.toast_style);
toastStyle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
if(checkId == R.id.info){
gMotionToastStyle = MotionToastStyle.INFO;
}else if(checkId == R.id.success){
gMotionToastStyle = MotionToastStyle.SUCCESS;
}else if(checkId == R.id.warning){
gMotionToastStyle = MotionToastStyle.WARNING;
}else if(checkId == R.id.error){
gMotionToastStyle = MotionToastStyle.ERROR;
}
}
});
//메시지 실행버튼 이벤트
Button createToast = findViewById(R.id.create_toast);
createToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String title = "title";
String message = "message";
int position = MotionToast.GRAVITY_BOTTOM; // GRAVITY_TOP,GRAVITY_CENTER
long duration = MotionToast.SHORT_DURATION; // LONG_DURATION
//메시지 생성
createToast(title, message, gMotionToastStyle, position, duration);
}
});
}//onCreate
/**
* 메시지 실행
* @param title 제목
* @param message 내용
* @param style 스타일
* @param position 위치
* @param duration 구동시간
*/
private void createToast(String title, String message, MotionToastStyle style,
int position, long duration) {
if(gToastType.equals("motion")){
MotionToast.Companion.createToast(MainActivity.this,
title,
message,
style,
position,
duration,
ResourcesCompat.getFont(getApplicationContext(), R.font.helvetica_regular));
}else if(gToastType.equals("colorMotion")){
MotionToast.Companion.createColorToast(MainActivity.this,
title,
message,
style,
position,
duration,
ResourcesCompat.getFont(getApplicationContext(), R.font.helvetica_regular));
}else if(gToastType.equals("dark")){
MotionToast.Companion.darkToast(MainActivity.this,
title,
message,
style,
position,
duration,
ResourcesCompat.getFont(getApplicationContext(), R.font.helvetica_regular));
}else if(gToastType.equals("darkColor")){
MotionToast.Companion.darkColorToast(MainActivity.this,
title,
message,
style,
position,
duration,
ResourcesCompat.getFont(getApplicationContext(), R.font.helvetica_regular));
}
}
} //MainActivity
2022.01.23 - [안드로이드] - [안드로이드] 색상조절막대 ColorSeekBar 쉽게 만드는 방법
2022.01.24 - [안드로이드] - [안드로이드] 진행률 ProgressView 쉽게 만드는 방법
2022.01.25 - [안드로이드] - [안드로이드] 플립시계(Flip_Digit) 쉽게 만드는 방법
[안드로이드] 검색한 단어 하이라이트(highlight) 주는 방법 (0) | 2022.01.28 |
---|---|
[안드로이드] 밀어서 날짜 변경하는 달력(SlideDatePicker) 쉽게 만드는 방법 (0) | 2022.01.27 |
[안드로이드] 플립시계(Flip_Digit) 쉽게 만드는 방법 (0) | 2022.01.25 |
[안드로이드] 진행률 ProgressView 쉽게 만드는 방법 (0) | 2022.01.24 |
[안드로이드] 색상조절막대 ColorSeekBar 쉽게 만드는 방법 (0) | 2022.01.23 |
댓글 영역