<표현> 속성 메소드 개념
둘째마당 03-3
- 코드 만들어서 살행 시 중간에 디버깅 메시지 확인 혹은 사용자에게 메시지 보여주어야 하는 경우 존재
- Log 클래스 통해 로그 출력 (안드로이드 스튜디오 Logcat ckd)
- 토스트 메시지
토스트
간단한 메시지를 잠깐 보여주었다가 없어지는 뷰
Toast.makeText(Context context, String message, int duration).show();
- 대화상자와 함께 사용자에게 필요한 정보를 알려주는 역할
- 포커스 받지 않으므로, 대화상자보다 더 쉽고 간단하게 사용 가능
- Context 객체
- Context 클래스 상속한 액티비티 사용 가능
- 액티비티 참조 어려운 경우, getApplicationContext() 메소드 호출하여 return Context 객체
- 토스트 위치, 여백 지정 메소드
- public void setGravity(int gravity, int xOffset, int yOffset)
- 토스트 뷰가 보이는 위치 지정
- gravity = Gravity.CENTER 같이 정렬 위치 지정
- public void setMargin(float horizontalMargin, float verticalMargin)
- 외부 여백 지정
- 값 이용하여 토스트 뷰 중앙이나 우측 하단에 배치 가능
- 토스트 위치 바꾸어 보여주기
-
public void onButton1Clicked(View v) { try { Toast toastView = Toast.makeText(this, "위치가 바뀐 토스트 메시지입니다. ", Toast.LENGTH_LONG); int xOffset = Integer.parseInt(editText.getText().toString()); int yOffset = Integer.parseInt(editText2.getText().toString()); toastView.setGravity(Gravity.TOP|Gravity.TOP, xOffset, yOffset); toastView.show(); } catch (NumberFormatException e) { e.printStackTrace(); } }
- 토스트 모양 바꾸어 보여주기
- LayoutInflater 객체 참조
- XML 레이아웃을 메모리에 로딩하는 데 사용
- 토스트를 위한 레이아웃 정의하면, LayoutInflater 객체 사용하여 직접 메모리에 객체화 필요
-
public void onButton2Clicked(View v) { LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toastborder, (ViewGroup) findViewById(R.id.toast_layout_root)); TextView text = layout.findViewById(R.id.text); Toast toast = new Toast(this); text.setText("모양 바꾼 토스트"); toast.setGravity(Gravity.CENTER, 0, -100); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(layout); toast.show(); }
- 토스트 위한 레이아웃 인플레이션
- 토스트 객체 생성하여 토스트 뷰 설정
- 자주 사용하는 경우 필요한 모양, 색사응로 변경하여 토스트 메시지 활용
- app/res/drawable/toast.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <stroke android:width="4dp" android:color="#ffffff00"/> <solid android:color="#ff883300"/> <padding android:left="20dp" android:top="20dp" android:right="20dp" android:bottom="20dp"/> <corners android:radius="15dp"/> </shape>
- app/res/drawable/toast.xml
- LayoutInflater 객체 참조
- public void setGravity(int gravity, int xOffset, int yOffset)
- Context 객체
스낵바
간단한 메시지 보여줄 때 토스트 대신 사용하는 경우도 많음
- 화면 아래쪽에서 올라와서 화면 일부분을 가리지만, 토스트와는 다른 방식으로 메시지 보여줌
-
public void onButton3Clicked(View v) { Snackbar.make(v, "스낵바입니다. ", Snackbar.LENGTH_LONG).show(); }
대화상자
사용자에게 확인 받거나 선택하게 할 때 사용
- 주로 일방적 메시지 전달하는 역할, '예' '아니오' 같은 전형적인 응답 처리
- AlertDialog.Builder builder = new AlertDialog.Builder(this);: 대화상자 만들기 위한 빌더 객체 생성
- 예, 취소, 아니오 버튼 추가
- 예: setPositiveButton()
- 취소: setNeutralButton()
- 아니오: setNegativeButton()
- AlertDialog dialog = builder.create();: 객체 생성 후 보여주기
-
private void showMessage() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("안내"); builder.setMessage("종료하시겠습니까?"); builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setPositiveButton("예", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String message = "예 버튼이 눌렸습니다. "; textView.setText(message); } }); builder.setNeutralButton("취소", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String message = "취소 버튼이 눌렸습니다. "; textView.setText(message); } }); builder.setNegativeButton("아니오", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String message = "아니오 버튼이 눌렸습니다. "; textView.setText(message); } }); AlertDialog dialog = builder.create(); dialog.show(); }
- 알림상자 설정
- setTitle(): 알림 대화상자 타이틀 설정
- setMessage(): 내용 설정
- setIcon()
- 기본 API에 포함된 아이콘 사용하는 경우, android.R.drawable
프로그레스바
어떤 일의 진행 정도 표시
- 종류
- 막대 모양
- 진행 정도 알려줌
- style = '?android:attr/progressBarStyleHorizontal'로 설정
- 원 모양
- 작업이 진행 중임을 알려줌
- 원 모양의 프로그레스바가 반복 표시
- 막대 모양
- 사용 메소드
- void setProgress(int progress): 정수 값 받아 프로그레스바의 현재 값으로 설정
- void incrementProgressBy(int diff): 현재 설정된 값 기준으로 값 더하거나 뺄 때 사용
- requestWindowFeature(Window.FEATURE_PROGRESS);
- 항상 보일 필요 없으므로 차지 공간 줄이도록 타이틀바에 표시
- 디폴트 값은 0부터 10000사이의 값
- 타이틀 부분 보이지 않게 설정하는 앱도 있어 사용할 수 없는 경우도 존재함
- 코드
- dialog = new ProgressDialog(MainActivity.this);
- 프로그레스 대화상자 객체 만들고 설정
- 객체 생성 시 Context 객체 파라미터로 전달해야 하여 MainActivity 객체 파라미터로 전달
- dialog.dismiss();
- 대화상자 보이지 않게 함
-
protected void onCreate(Bundle savedInstanceState) { ... ProgressBar progressBar = findViewById(R.id.progressBar); progressBar.setIndeterminate(false); progressBar.setProgress(80); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog = new ProgressDialog(MainActivity.this); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); dialog.setMessage("데이터를 확인하는 중입니다. "); dialog.show(); } }); Button button2 = findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (dialog != null) { dialog.dismiss(); } } }); }
- dialog = new ProgressDialog(MainActivity.this);
'Android ᙏ̤̫͚ > Android Programming' 카테고리의 다른 글
[안드로이드] SVG 아이콘 사용하기 (0) | 2021.09.05 |
---|---|
[Do it 안드로이드 프로그래밍] 둘째마당 04. 여러 화면 간 전환하기 (0) | 2021.07.25 |
[Do it 안드로이드 프로그래밍] 둘째마당 03-2. 드로어블, 이벤트 처리 (0) | 2021.07.17 |
[Do it 안드로이드 프로그래밍] 둘째마당 03-1. 기본 위젯 (0) | 2021.07.17 |
[Do it 안드로이드 프로그래밍] 둘째마당 02. 레이아웃 (0) | 2021.07.08 |