상세 컨텐츠

본문 제목

[안드로이드]커스텀 다이얼로그(Custom Dialog) 사용하기.

Android.. Story

by HeyLee 2011. 4. 13. 18:13

본문


오랜만에 또 포스트를 쓰는데 이번에는.. Dialog에 대해서 써볼까합니다.
 
다이얼로그는 구글이 제공한 오픈 소스 demo에서 소스가 잘나와있습니다.
여러가지 예시를 해놓고 여러가지 모양으로 보여주는데 구글이 제공한 모양으로만 쓰기 참 그렇죠..=_=

앱 제작하는데...배경은 화사롭거나 효과 많이 들어간 이미지로 도배를 해놨는데 다이얼로그 창을띄우는데..엇..

구글이 기본제공하는 이미지 딸랑뜨고... 이러면 유저 입장에서 ..."이게 뭥가요" 라는 느낌이 듭니다.

그런 느낌을 주지 않을려면 개발자가 이쁘게 다이얼로그 창을 커스텀해서 사용해야겠죠?.

커스텀 다이얼로그를 쓰는법도 어려운게 아닙니다.

기존 제공 다이얼로그를 쓰는 것은 아래와 같이 AlertDiagDialog 하위 메소드에 값을 넣고

마지막에 .create()를 실행하여 다이얼로그 창을 띄우게 됩니다.

new AlertDialog.Builder(AlertDialogSamples.this)

                .setIcon(R.drawable.alert_dialog_icon)

                .setTitle(R.string.alert_dialog_two_buttons_msg)

                .setMessage(R.string.alert_dialog_two_buttons2_msg)

                .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {

                            /* User clicked OK so do some stuff */

                    }

                })

                .setNeutralButton(R.string.alert_dialog_something, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Something so do some stuff */

                    }

                })

                .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int whichButton) {

                        /* User clicked Cancel so do some stuff */

                    }

                })

                .create();



위와 같이 코드를 짠다면 다음과같은 화면을 볼수가있습니다. 디폴트 식의 다이얼로그 띄우기 입니다.


물론 프로그레스바나 리스트 같은것도 넣을수있습니다.
하지만 전체적으로 틀을 바꿀려면 LayoutInflater를 이용해야됩니다.

LayoutInflater는 xml을 이용해서 새로운 뷰를 구성할떄 사용되는 클래스입니다. 
새로운 뷰구성에 쓰이는 메소드 inflate를 이용하면 됩니다. 

public View inflate (int resource, ViewGroup root)

Since: API Level 1

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters
resource ID for an XML layout resource to load (e.g., R.layout.main_page)
root Optional view to be the parent of the generated hierarchy.
Returns
  • The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.

아래와 같이 코딩해서 커스텀 다이얼로그를 구성하면 OK.

public void dialog(){
        
Context DialogContext=SearchWineActivity.this;

        AlertDialog.Builder builder;

Context mContext=TestActivity.this;
        LayoutInflater inflater=(LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.dialog, null);

//-------------------------------
content
추가. dialog.xml에 있는것은 다이얼로그에 속하지 않은 뷰입니다.
그래서 findViewById앞에 view인자를 써주셔야 합니다.(주의)

ex)
SeekBar mSeekBarPenWidth = (SeekBar) layout.findViewById(R.id.seekbarpenwidth); 

//------------------------------- 


builder=new AlertDialog.Builder(DialogContext);

alertDialog=builder.create();

        alertDialog.show();
        alertDialog.setContentView(layout);

관련글 더보기

댓글 영역