본문 바로가기

구글과인터넷/안드로이드

안드로이드 다이얼로그 관련, [Android Dev.]AlertDialog 활용하기

다이얼로그 삭제 관련
int dlgid = id;

Activity  ->   removeDialog(dlgid);

///////////////////////////////////////////////////////

출처: http://jcjeon.tistory.com/32


User Interface - Creating Dialog [02]Android - Google/DEV-FW-Topics 2010/02/09 23:16

Creating Dialogs

Dialog는 보통 현재 Activity의 앞에 보여지는 작은 윈도우이다. 아래에 있는 Activity는 Focus를 잃고 Dialog가 사용자와의 모든 상호작용을 받아들이게 된다. Dialgos는 일반적으로 Progress상에서 Application과 직접 관계된 짧은 시간동안의 Activities과 알림을 위해 사용된다.

Android API는 다음의 Dialog Objects의 Types을 지원한다 :

AlertDialog : 대부분의 Dialog UI를 제공해 줌
ProgressDialog : Progress Wheel or Progress Bar
DatePickerDialog : 날짜를 설정
TimePickerDialog : 시간을 설정




Showing a Dialog

Dialog는 항상 Activity의 일부로 생성되어 보여진다. 보통 onCreateDialog(int) Callback Method를 사용하여 Activity내부에서 Dialog를 생성해야 한다. 이러한 Callback을 사용할 때, Android는 자동으로 Dialog의 상태를 관리하고 Dialogs을 Activity에 걸어주어 효과적으로 각각의 Dialog의 "Owner"를 만들게 된다. 그렇게 함으로써, 각각의 Dialog는 Activity로부터 확정된 Properties를 상속받게 된다. 예를 들어, Dialog가 Open되었을 때, 메뉴 key는 Activity에 정의되어 있는 Option Menu를 보여주며 Volume Key는 Activity에 의해 사용되는 오디어 스트림을 조절할 수 있다.

Note : 만약 onCreateDialog() method외부에 Dialog를 생성하면 Activity에 붙여지지 않게 되지만 setOwnerActivity(Activity)를 통해 Activity로 붙여줄 수 있다.

Dialog를 보여주고 싶다면 showDialog(int)를 호출하고 보여주고 싶은 Dialog를 구분해주는 번호를 넘겨주면 된다.

Dialog가 처음 요청받게 되면, Android는 onCreateDialog(int)를 호출하는데 그곳에서 Dialog를 인스턴스화 해줘야 한다. 이러한 Callback Method는 showDialog(int)로 받은 것과 동일한 ID를 넘겨받는다. Dialog가 만들어 진 후, Method의 맨 끝에서 해당 Object를 반환하게 된다. 

Dialog가 보여지기 이전에, Android는 선택적 Callback Method인 onPrepareDialog(int, Dialog)를 호출한다. Dialog가 열릴 때 Dialog의 Property를 바꾸고 싶다면 이러한 Method를 정의해야 한다. 이러한 Method는 Dialog가 열릴 때마다 호출되는 반면, onCreateDialog(int)는 Dialog가 처음 열릴 때 한번만 호출된다. 만일 onPrepareDialog()를 정의하지 않았다면 Dialog는 이전 상태를 그대로 유지한다. 이러한 Method는 onCreateDialog()를 통해 만들어진 Dialog Object와 함께 Dialog의 ID를 넘겨받는다.

onCreateDialog(int)와 onPrepareDialog(int, Dialog) Callback Methods를 정의하는 가장 최선의 방법은 Method로 넘겨지는 "id" Parameter를 체크하는 "switch" 구문을 사용하는 것이다. 각각의 "case"는 Dialog ID를 체크하고 해당하는 Dialog를 생성하고 정의한다.

먼저, 각각의 Dialog에 대한 ID를 정의한다.

static final int DIALOG_PAUSED_ID = 0;
static final int DIALOG_GAMEOVER_ID = 1;


그 이후, onCreateDialog(int) Callback을 정의한다.

protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
    case DIALOG_PAUSED_ID:
        // do the work to define the pause Dialog
        break;
    case DIALOG_GAMEOVER_ID:
        // do the work to define the game over Dialog
        break;
    default:
        dialog = null;
    }
    return dialog;
}



Dismissing a Dialog
Dialog를 닫고 싶을 때 Dialog Object에서 dismiss()를 호출하여 종료시킬 수 있으며 필요시 dismissDialog(int)를 호출할 수 있다.

Dialog의 State 관리를 위해 onCreateDialog(int)를 사용한다면 Dialog가 닫힐때마다 Dialog Object의 State은 Activity에 의해 유지된다. 만일 이러한 Object가 더이상 필요없거나 State가 Clear되는 것이 중요하다면 removeDialog(int)를 호출하면 된다. 이것은 Object에 연결된 어떤 내부 참조들이라도 삭제하게 되며 만일 현재 보여지고 있으면 닫아주게 된다.


Using Dismiss Listeners

만약 Dialog가 닫히는 그 시점에 Appliction이 몇몇 Procedures를 수행하고 싶다면 Dialog에 on-dismiss listener를 붙여놓아야  한다. 

우선 DialogInterface.onDismissListener Interface를 정의한다. 이러한 Interface는 onDismiss(DialogInterface) Method 하나만을 가지고 있으며 Dialog가 닫힐 때 호출된다. 그 다음 setOnDismissListener()에 구현된 OnDismissListener를 넘겨주면 된다. 

그러나, "canceled" 될 수 도 있음을 알아야 한다. 이것은 특별한 경우로 Dialog가 User에 의해 명시적으로 취소될때를 가리킨다. 이것은 사용자가 "back" Key를 눌렀을 때나 Dialog내에 있는 Cancel 버튼으로 cancel()이 호출되었을 때 발생하게 된다. Dialog가 취소되었을 때 OnDismissListener은 알림을 기다리게 되지만 Dialog가 명시적으로 취소 되었음을 알려주고 싶다면 setOnCancelListen()를 DialogInterface.OnCancelListener로 등록시켜야 한다.


Creating an AlertDialog

AlertDialog는 Dialog Class의 확장버전이다. 대부분의 Dialog UI를 구성할 수 있고 권장되는 Dialog Type이다. 다음과 같은 특징중 하나를 가지는 Dialog를 위해서는 AlertDialog를 사용해야 한다 :

  Title
  Text Message
  One, Two, or Three Buttons
  List of Selectable Items(Optional Checkboxes, or Radio Buttons)

AlertDialog를 생성하려면 AlertDialog.Builder subclass를 사용한다. AlertDialog.Builder(Context)를 통해 Builder를 얻어와 AlertDialog Properties의 모두를 정하는 클래스의 Public Methods을 사용한다. Builder로 모든 작업이 끝난 이후, create()을 통해 AlertDialog Object를 얻어온다.

다음의 주제는 AlertDialog.Builder Class를 이용하여 AlertDialog의 다양한 Properties을 어떻게 정의하는지를 보여준다. onCreateDialog() Callback Method내에 다음의 예제 코드의 일부를 사용하고자 한다면 Dialog를 보여주기 위한 Dialog Object의 결과물을 반환할 수 있다. 


Adding Buttons
아래 그림과 같이 버튼을 나란히 가지는 AlertDialog를 생성하기위해서는 set...Button() Methods를 사용한다.







AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
        .setCancelable(false)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                MyActivity.this.finish();
           }
       })
       .setNegativeButton("No", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
           }
       });
AlertDialog alert = builder.create();


Adding a List






final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();


Adding Checkboxes and Radio Buttons





final CharSequence[] items = {"Red", "Green", "Blue"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();



Creating a ProgressDialog







ProgressDialog는 AlertDialog Class를 확장한 것으로, 정의된 진행과정이 없을 경우 Spinning Wheel의 형태로 애니메이션 프로그레스를 보여줄 수 있고 정해진 진행과정을 가지는 Task를 위해 Progress Bar의 형태로 보여줄 수 있다. Dialog는 Download를 취소할 수 있는 것과 같이 버튼을 제공할 수 있다.
Progress Dialog를 여는 것은 간단하게 ProgressDialog.show()만 호출하면 된다.
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
                                  "Loading. Please wait...", true);


Showing a Progres Bar






애니메이션 형태의 Progress Bar를 보여주기 위해서는 : 
1. ProgressDialog(Context) 생성자를 통해 ProgressDialog를 초기화 한다.
2. setProgressStyle(int)를 통해 "STYLE_HORIZONTAL"로 Style을 설정하고 그런 메시지를 이용하여 다른 Properties도 설정한다.
3. Dialog를 보여줄 준비기 되었다면, show()를 호출하거나 onCreateDialog(int) Callback을 통해 ProgressDialog를 반환시켜 준다.
4. 전체 진행 과정이 완료되기 까지 setProgress(int)를 호출하여 Bar에 보여지는 상태 진행정도를 증가 시킬수 있으며 incrementProgressBy(int)를 통해서도 가능하다.

ProgressDialog progressDialog;progressDialog = new ProgressDialog(mContext);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMessage("Loading...");
progressDialog.setCancelable(false);


Creating a Custom Dialog





1. custom_dialog.xml으로 저장된 XML Layout을 생성한다.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="10dp"
              >
    <ImageView android:id="@+id/image"
               android:layout_width="wrap_content"
               android:layout_height="fill_parent"
               android:layout_marginRight="10dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:textColor="#FFF"
              />
</LinearLayout>

2. Dialog의 Content View로 위에 정의해 둔 Layout을 설정하고 ImageView와 TextView 요소를 위한 내용을 정의한다.
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);

3. 이것이 전부다. 앞에서 설명했던 "Showing a Dialog"에서 처럼 보여주기만 하면 된다.

기본 Dialog Class를 기반으로 만들어진 Dialog는 반드시 제목을 가져야 한다. 만약 setTitle()을 호출하지 않으면 제목에 대한 공간은 비어있게 되지만 눈에는 보이는 상태가 된다. 만일 Title이 전혀 필요없다면 AlertDialog Class를 사용하여 사용자 Dialog를 만들어야만 한다. 그러나, AlertDialog는 AlertDialog.Builder Class에 의해 간단히 생성되기 때문에 setContentview(int) Method에 접근할 필요가 없어진다. 그대신, setView(View)를 반드시 사용해야만 하는데 이러한 Method는 View Object를 용인하여 XML로부터의 Root View Object를 Inflating시킬 필요가 있게 된다.

XML Layout을 Inflating시키기 위해서는 LayoutInflater를 얻어와 inflate(int, ViewGroup)을 호출해야 한다. 여기서 첫번째 파라미터는 Layout Resource ID이고 두번째 파라미터는 Root View의 ID이다. 이때, Layout에 있는 View Objects를 찾는데 Inflated Layout을 사용할 수 있고 ImageView와 TextView에 대한 내용을 정의할 수 있다. 그 다음 AlertDialog.Builder를 인스턴스화하고 setView(View)를 통해 해당 Dialog를 위한 Inflated Layout을 설정한다.

AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();




///////////////////////////////////////////////////////////////////////////////////////

출처: http://neodreamer.tistory.com/411

AlertDialog 는 사용자에게 메세지나 경고를 알리기 위한 기능으로 Android 에서 지원하는 Dialog 이다. Toast 와는 다르게 Dialog 라서 Activity의 Focus를 가져간다.

대부분의 Dialog 는 Activity의 onCreateDialog() 함수에서 생성하는 것을 권장하고 있다. onCreateDialog() 에서 생성된 Dialog 는 Activity 에서 관장하고 있으며 Activity 객체들()을 상속 받는다. 
showDialog() 가 호출되면 생성되지 않은 Dialog의 경우 onCreateDialog() 함수에서 생성이 되고 이후에 호출이 되어질 때에는 새로 생성하지 않고 기존 인스턴스를 그대로 이용한다. 다이얼로그가 호출되기 전에 Dialog 에 변화를 주려면 onPrepareDialog() 함수에서 처리 한다. onCreateDialog 에서 생성한 Dialog는 Activity 에서 항상 갖고 있는 객체로 더이상 필요가 없을 경우에는 시스템 시소스 확보를 위해 removeDialog() 를 호출하여 제거해 주는 것이 좋다.

간단하게 사용자에게 경고를 하기 위한 Dialog 라면 시스템이 관리하지 않고 그냥 간단하게 사용하는 것이 리소스 관리 측면에서 더 유용해 보인다.

AlertDialog 는 사용자에게 아주 간단하고 쉽게 경고 메세지를 보여 줄 수 있는 Dialog 이다. 하지만 Custom View 까지 지원하여 아주 많은 것을 표현해 줄 수 있다.

AlertDialog 는 아래의 요소를 갖을 수 있다.
  • Icon
  • Title
  • Message or ListView (두가지를 함께 지원하지 않음)
  • Custom View
  • Button (최대 3개)







Message 를 갖는 AlertDialog




ListView 를 갖는 AlertDialog







AlertDialog 가 갖을 수 있는 모든 요소는 필요 요소가 없다. 모든 요소를 제거하고 AlertDialog 를 호출하면 Activity의 Focus를 가져오고 화면에는 아무것도 표시가 되지 않는다.

AlertDialog 를 생성하는 방법은 AlertDialog.Builder 클래스를 이용하면 된다.

아주 간단한 AlertDialog 생성하기





간단한 AlertDialog


01AlertDialog alert = new AlertDialog.Builder( this )
02    .setIcon( R.drawable.icon )
03    .setTitle( "AlertTitle" )
04    .setMessage( "AlertMessage" )
05    .setPositiveButton( "OK"new DialogInterface.OnClickListener()
06    {
07        @Override
08        public void onClick(DialogInterface dialog, int which)
09        {
10            dialog.dismiss();
11        }
12    })
13    .show();


구성 요소중 Icon 과 Title 을 없애면 두 영역이 사라지지만 Icon 없이 Title 을 지정하면 기본 Icon 이 표시된다.




Icon과 Title을 제거한 AlertDialog



Icon 없이 Title 만 지정한 AlertDialog


AlertDialog 에서 제공하는 ListView 활용하기
AlertDialog.Builder 에 있는 setSingleChoiceItems 함수를 이용하며 AlertDialog 에서 제공하는 ListView 를 사용할 수 있다.




ListView를 갖는 AlertDialog


01String[] strItems = { "Alert Item 1""Alert Item 2""Alert Item 3" };
02 
03AlertDialog alert = new AlertDialog.Builder( this )
04    .setIcon( R.drawable.icon )
05    .setTitle( "AlertTitle" )
06    .setPositiveButton( "OK"new DialogInterface.OnClickListener()
07    {
08        @Override
09        public void onClick(DialogInterface dialog, int which)
10        {
11            dialog.dismiss();
12        }
13    })
14    .setNeutralButton( "Neutral"new DialogInterface.OnClickListener()
15    {
16        @Override
17        public void onClick(DialogInterface dialog, int which)
18        {
19 
20        }
21    })
22    .setNegativeButton( "Cancel"new DialogInterface.OnClickListener()
23    {
24        @Override
25        public void onClick(DialogInterface dialog, int which)
26        {
27 
28        }
29    })
30    .setSingleChoiceItems(strItems, -1new DialogInterface.OnClickListener()
31    {
32        @Override
33        public void onClick(DialogInterface dialog, int which)
34        {
35            String msg = "";
36            switch ( which )
37            {
38            case 0: msg = "Item 1 Selected"break;
39            case 1: msg = "Item 2 Selected"break;
40            case 2: msg = "Item 3 Selected"break;
41            }
42            Toast.makeText( MyDialog.this, msg, Toast.LENGTH_SHORT ).show();
43        }
44    })
45    .show();



Custom View 를 얹은 AlertDialog




Custom View 를 얹은 AlertDialog


Custom View XML
01<?xml version="1.0" encoding="utf-8"?>
02<TableLayout
04  android:layout_width="wrap_content"
05  android:layout_height="wrap_content">
06  
07    <TableRow>
08        <ImageView
09            android:id="@+id/IVIcon1"
10            android:src="@drawable/icon"
11            />
12        <TextView
13            android:id="@+id/TVAndroid1"
14            android:text="Android 1"
15            />
16    </TableRow>
17     
18    <TableRow>
19        <ImageView
20            android:id="@+id/IVIcon1"
21            android:src="@drawable/icon"
22            />
23        <TextView
24            android:id="@+id/TVAndroid1"
25            android:text="Android 1"
26            />
27    </TableRow>  
28</TableLayout>

01LayoutInflater layout2 = getLayoutInflater();
02View v2 = layout2.inflate( R.layout.alert_custom_view, null );
03 
04AlertDialog alert = new AlertDialog.Builder( this )
05    .setIcon( R.drawable.icon )            
06    .setTitle( "AlertTitle" )
07    .setMessage( "AlertMessage" )
08    .setPositiveButton( "OK"new DialogInterface.OnClickListener()
09    {                      
10        @Override
11        public void onClick(DialogInterface dialog, int which)
12        {
13            dialog.dismiss();
14        }
15    })
16    .setNeutralButton( "Neutral"new DialogInterface.OnClickListener()
17    {
18        @Override
19        public void onClick(DialogInterface dialog, int which)
20        {
21             
22        }
23    })
24    .setNegativeButton( "Cancel"new DialogInterface.OnClickListener()
25    {
26        @Override
27        public void onClick(DialogInterface dialog, int which)
28        {
29             
30        }
31    })
32    .setView( v2 )
33    .show();