본문 바로가기
Android

Android Dialog

by 캡틴노랑이 2021. 4. 2.
반응형

Android Dialog로 사용가능한 모든 예제...

 

더 있나??

 

test device : note 4

android os : Marshmallow 6.0

 

 

 

 

 

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<code>package com.example.sampleproject;
 
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
 
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
 
import com.example.sampleproject.Norang.NorangActivity;
 
import java.util.ArrayList;
import java.util.List;
 
 
//https://lktprogrammer.tistory.com/155
public class DialogActivity extends NorangActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        InitializeControl();
        InitializeCommonDataBind();
    }
 
    private void InitializeCommonDataBind() {
 
    }
 
    private void InitializeControl() {
        toolbar = findViewById(R.id.tbToolBar);
        setToolbar("");
    }
 
 
    public void btnButtonOnClick(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("sample button add");
        builder.setMessage("set message");
 
        builder.setPositiveButton("button 1", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                Toast.makeText(getApplicationContext(), "button1", Toast.LENGTH_SHORT).show();
            }
        });
 
        builder.setNegativeButton("button 2", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                Toast.makeText(getApplicationContext(), "button2", Toast.LENGTH_SHORT).show();
            }
        });
 
        builder.setNeutralButton("button 3", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int id)
            {
                Toast.makeText(getApplicationContext(), "button3", Toast.LENGTH_SHORT).show();
            }
        });
 
        AlertDialog alert = builder.create();
        alert.show();
    }
 
 
    public void btnDlgResStringOnClick(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Alert List");
 
        //res/values/strings/alertList 목록을 가져옴.
        builder.setItems(R.array.alertList, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int pos) {
                String [] items = getResources().getStringArray(R.array.alertList);
                Toast.makeText(getApplicationContext(), items[pos], Toast.LENGTH_LONG).show();
            }
        });
        //builder.setCancelable(false);    // 뒤로 가기 버튼 사용 금지
        AlertDialog alert = builder.create();
        alert.show();
    }
 
    public void btnDlgListAddOnClick(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Alert List");
 
        List lst = new ArrayList();
        lst.add("test1");
        lst.add("test2");
        lst.add("test3");
        lst.add("test4");
 
        final CharSequence[] items = lst.toArray(new CharSequence[lst.size()]);
 
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int pos) {
                //String [] items = getResources().getStringArray(R.array.alertList);
                Toast.makeText(getApplicationContext(), items[pos], Toast.LENGTH_LONG).show();
            }
        });
        //builder.setCancelable(false);    // 뒤로 가기 버튼 사용 금지
        AlertDialog alert = builder.create();
        alert.show();
    }
 
    public void btnMultiChkOnClick(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Multi check List");
 
        final ArrayList lstSelectedItems = new ArrayList();
        final String[] items = getResources().getStringArray(R.array.alertList);
 
 
        builder.setMultiChoiceItems(R.array.alertList, null, new DialogInterface.OnMultiChoiceClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int pos, boolean isChecked)
            {
                if(isChecked == true)
                    lstSelectedItems.add(items[pos]);
                else
                    lstSelectedItems.remove(pos);
            }
        });
 
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int pos)
            {
                String seletedItems = "";
 
                for(int i =0; i<lstSelectedItems.size(); i++)
                    seletedItems += (seletedItems.equals("") ? "" : ",")  + lstSelectedItems.get(i);
 
                Toast toast = Toast.makeText(getApplicationContext(), "Selected Item :" + seletedItems, Toast.LENGTH_LONG);
                //toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });
 
 
        //builder.setCancelable(false);    // 뒤로 가기 버튼 사용 금지
        AlertDialog alert = builder.create();
        alert.show();
    }
 
 
    public void btnSingleSelectOnClick(View view)
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Multi check List");
 
        final String[] items = getResources().getStringArray(R.array.alertList);
        final String[] selected = {""};
 
 
        builder.setSingleChoiceItems(R.array.alertList, 0, new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int pos)
            {
                selected[0] = items[pos];
            }
        });
 
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int pos)
            {
                Toast toast = Toast.makeText(getApplicationContext(), "Selected Item : " + selected[0], Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
            }
        });
 
 
        //builder.setCancelable(false);    // 뒤로 가기 버튼 사용 금지
        AlertDialog alert = builder.create();
        alert.show();
    }
 
    public void btnCustomDlgOnClick(View view)
    {
        View dialogView = getLayoutInflater().inflate(R.layout.activity_dialog_sub, null);
 
        final EditText etTxt1 = (EditText)dialogView.findViewById(R.id.etTxt1);
        final EditText etTxt2 = (EditText)dialogView.findViewById(R.id.etTxt2);
 
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialogView);
 
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int pos)
            {
                Toast.makeText(getApplicationContext(),etTxt1.getText() + "\n" + etTxt2.getText(), Toast.LENGTH_LONG).show();
            }
        });
 
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }
}
</code>

 

 

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
   <?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=".DialogActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/tbToolBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:title="Main" />
    <Button
        android:id="@+id/btnButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="btnButtonOnClick"
        />
    <Button
        android:id="@+id/btnDlgResString"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="From Resource"
        android:onClick="btnDlgResStringOnClick"
        />
    <Button
        android:id="@+id/btnDlgListAdd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="From List"
        android:onClick="btnDlgListAddOnClick"
        />
    <Button
        android:id="@+id/btnMultiChk"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Multi selected"
        android:onClick="btnMultiChkOnClick"
        />
 
    <Button
        android:id="@+id/btnSingleSelect"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Single selected"
        android:onClick="btnSingleSelectOnClick"
        />
    <Button
        android:id="@+id/btnCustomDlg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Custom alert"
        android:onClick="btnCustomDlgOnClick"
        />
 
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>     

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
 
    <EditText
        android:id="@+id/etTxt1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="etTxt1" />
    <EditText
        android:id="@+id/etTxt2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="etTxt2" />
</LinearLayout>     

 

 

 

 

 

 

 

 

 

 

 

 

반응형