본문 바로가기
안드로이드 자바

[Java][Android] Material Design Alert Dialog 사용하기

by teamnova 2024. 12. 2.
728x90

안녕하세요

 

오늘은 Material Design에서 제공하는 Alert Dialog를 사용해보도록 하겠습니다.

 

전체 코드입니다.

 

MainActivity.java

public class MainActivity extends AppCompatActivity {

  private Button btn_일반;
  private Button btn_MD;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn_MD = findViewById(R.id.btn_MD);
    btn_일반 = findViewById(R.id.btn_일반);

    btn_일반.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        new AlertDialog.Builder(MainActivity.this)
            .setTitle("알림")
            .setMessage("일반 Alert Dialog")
            .setPositiveButton("확인", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {

              }
            })
            .setNegativeButton("취소", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {

              }
            })
            .show();
      }
    });

    btn_MD.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        new MaterialAlertDialogBuilder(MainActivity.this)
            .setTitle("알림")
            .setMessage("Material Design Alert Dialog")
            .setPositiveButton("확인", (dialog, which) -> {

            })
            .setNegativeButton("취소", (dialog, which) -> {

            })
            .show();

      }
    });

  }

}

 

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">
    
    <Button
      android:id="@+id/btn_일반"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="일반 다이얼로그"
      android:layout_marginTop="200dp"
      app:layout_constraintTop_toTopOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
        />

    <Button
      android:id="@+id/btn_MD"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="MD 다이얼로그"
      android:layout_marginTop="30dp"
      app:layout_constraintTop_toBottomOf="@id/btn_일반"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      />

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

Material Design에서 제공하는 다이얼로그와, 일반 다이얼로그를 생성하는 버튼을 만들었습니다.

 

MaterialAlertDialogBuilder Material Design 가이드라인에 따라 디자인 일관성 미려한 UI를 제공하고, 최신 Android 앱에서 일반적으로 사용하는 방식입니다.

 

AlertDialog.Builder는 더 간단한 스타일의 기본 다이얼로그를 표시하며, Material UI가 적용되지 않은 기본 Android 대화상자 스타일입니다.

 

시연 영상입니다.