ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [8] Dialog
    Develpment/Android Using Sample 2020. 9. 13. 21:36

    1. Dialog


     Layout

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.sin.custombutton.MainActivity"
        android:orientation="vertical">
     
        <Button
            android:id="@+id/show_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Show Dialog"/>
     
    </LinearLayout>
    cs

    * dialog 테스트를 위한 버튼 추가. (activity_main.xml)


     Layout

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">
     
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Dialog"/>
     
        <ImageView
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:layout_gravity="center"
            android:src="@mipmap/ic_launcher"/>
     
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="horizontal">
     
            <Button
                android:id="@+id/yes_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Yes"/>
     
            <Button
                android:id="@+id/no_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="no"/>
     
        </LinearLayout>
     
    </LinearLayout>
    cs

    * dialog로 띄우고 싶은 화면 구성.

    * res/layout 에 추가하여 작성. (dialog_activity.xml)


     Activity

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    public class MainActivity extends AppCompatActivity {
     
        Button show_btn;
        Button yes_btn, no_btn;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            show_btn = (Button)findViewById(R.id.show_btn);
            show_btn.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Dialog dialog = new Dialog(MainActivity.this);
     
                    dialog.setContentView(R.layout.dialog_activity);
     
                    dialog.setTitle("Dialog Title");
     
                    dialog.show();
                }
            });
        }
    }
    cs

    * Dialog 로 객체 생성.

    * setContentView 에 Dialog로 띄울 layout을 넣음.

    * setTitle 로 타이틀 설정.

    * show()로 화면에 보여준다.



    2. 결과 화면


     



    3. Dialog Button Event 처리


    Activity 

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    public class MainActivity extends AppCompatActivity {
     
        Button show_btn;
        Button yes_btn, no_btn;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            show_btn = (Button)findViewById(R.id.show_btn);
            show_btn.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Dialog dialog = new Dialog(MainActivity.this);
     
                    dialog.setContentView(R.layout.dialog_activity);
     
                    yes_btn = (Button)dialog.findViewById(R.id.yes_btn);
                    no_btn = (Button)dialog.findViewById(R.id.no_btn);
     
                    yes_btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            // yes Event 처리
                        }
                    });
                    no_btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            dialog.dismiss();   // dialog exit
                        }
                    });
     
                    dialog.setTitle("Dialog Title");
     
                    dialog.show();
                }
            });
        }
    }
    cs

    * dialog의 버튼은 dialog.findViewById 로 검색해서 처리.

    * dismiss() 호출시 dialog가 닫힌다.



    4. 추가 옵션 처리


     Activity

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    public class MainActivity extends AppCompatActivity {
     
        Button show_btn;
        Button yes_btn, no_btn;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            show_btn = (Button)findViewById(R.id.show_btn);
            show_btn.setOnClickListener( new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    final Dialog dialog = new Dialog(MainActivity.this);
     
                    //다이얼로그의 타이틀바 제거, SetContentView() 보다 위에서 해야한다.
                    dialog.requestWindowFeature( Window.FEATURE_NO_TITLE );
                    //다이얼로그 반투명 효과 제거
                    dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                    //다이얼로그 배경 제거
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
     
                    dialog.setContentView(R.layout.dialog_activity);
     
                    //뒤로 가기 및 주변터치로 다이얼 로그 종료되는것 막기
                    dialog.setCancelable(false);
     
                    dialog.setTitle("Dialog Title");
     
                    dialog.show();
                }
            });
        }
    }
    cs

    * 타이틀바 제거, 반투명 효과 제거, 다이얼로그 배경 제거 등의 옵션은 setContentView 이전에 호출 해야된다.

    * setCancelable( false ) 로 주변 클릭시 다이얼로그가 닫히는 것을 방지 할수 있다.



    5. 결과 화면


     



    'Develpment > Android Using Sample' 카테고리의 다른 글

    [10] Intent  (0) 2020.09.13
    [9] Rating Bar  (0) 2020.09.13
    [7] Alert Dialog  (0) 2020.09.13
    [6] Menu  (0) 2020.09.13
    [5] Image Button Effect (drawable)  (0) 2020.09.13

    댓글

Designed by Tistory.