이 튜토리얼에서는 안드로이드에서 모서리가 둥근 사용자 정의(커스텀) 다이얼로그를 만드는 방법을 단계별로 알아봅니다. 기본 AlertDialog의 각진 모서리 대신 부드러운 둥근 모서리를 적용하려면 커스텀 스타일과 drawable 배경을 활용해야 하는데, 그 과정을 코드 예제와 함께 자세히 설명합니다.
1단계: 새 프로젝트 생성
Android Studio에서 File → New Project로 이동한 후, 필요한 모든 정보를 입력하여 새 프로젝트를 생성합니다.
2단계: 메인 레이아웃 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/customDialog"
android:text="Custom Dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>위 코드에서는 버튼 하나를 배치했습니다. 사용자가 이 버튼을 클릭하면 커스텀 다이얼로그가 화면에 나타나게 됩니다.
3단계: MainActivity 구현
src/MainActivity.java 파일에 아래 코드를 추가합니다.
package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.customDialog).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.CustomAlertDialog);
ViewGroup viewGroup = findViewById(android.R.id.content);
View dialogView = LayoutInflater.from(v.getContext()).inflate(R.layout.customview, viewGroup, false);
Button buttonOk=dialogView.findViewById(R.id.buttonOk);
builder.setView(dialogView);
final AlertDialog alertDialog = builder.create();
buttonOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
});
}
}커스텀 다이얼로그를 표시하기 위해 별도의 뷰를 인플레이트(inflate)하여 다이얼로그에 설정했습니다. 이제 customview.xml 파일을 생성하고 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Success"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Headline" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla eu erat tincidunt lacus fermentum rutrum."
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
<Button
android:id="@+id/buttonOk"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="@color/colorPrimary"
android:text="Ok"
android:textColor="#FFF" />
</LinearLayout>
</LinearLayout>위 레이아웃에는 제목 텍스트, 본문 내용, 확인 버튼으로 구성된 다이얼로그 화면이 정의되어 있습니다.
4단계: 커스텀 테마 적용하기
다이얼로그에 둥근 모서리를 적용하려면 커스텀 테마를 지정해야 합니다. MainActivity에서 다음과 같이 두 번째 인자로 스타일을 전달합니다.
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,R.style.CustomAlertDialog);
이제 res/values/styles.xml 파일에 아래와 같이 스타일 태그를 추가합니다.
<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowBackground">@drawable/popup_background</item>
</style>위 코드에서는 다이얼로그의 창 배경(windowBackground)을 popup_background로 지정했습니다. 따라서 drawable 폴더 안에 popup_background.xml 파일을 생성하고 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF" />
<corners android:radius="6dp" />
</shape>핵심은 <corners> 태그입니다. 여기서 radius 값을 조절하면 다이얼로그 모서리의 둥근 정도를 원하는 대로 변경할 수 있습니다. 값이 클수록 더 둥글어집니다.
5단계: 앱 실행 및 결과 확인
애플리케이션을 실행해 보겠습니다. 실제 안드로이드 모바일 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바에서 Run 아이콘을 클릭하세요. 그다음 실행할 모바일 기기를 선택하면 기기에 기본 화면이 표시됩니다.

위 결과는 초기 화면입니다. 이제 버튼을 클릭하면 아래와 같이 모서리가 둥근 커스텀 다이얼로그가 열립니다.

정리
모서리가 둥근 커스텀 다이얼로그를 만드는 핵심 단계는 다음과 같습니다.
① 다이얼로그에 사용할 커스텀 레이아웃(XML)을 작성한다.
② AlertDialog.Builder 생성 시 커스텀 스타일(R.style.CustomAlertDialog)을 지정한다.
③ styles.xml에서 windowBackground 속성으로 둥근 모서리 shape drawable을 연결한다.
④ drawable에서 corners radius 값을 조절해 원하는 곡률을 적용한다.
이 방법을 응용하면 버튼 색상, 텍스트 스타일, 배경색 등을 자유롭게 변경하여 앱의 디자인에 어울리는 다양한 다이얼로그를 손쉽게 구현할 수 있습니다.