2
2
3
3
import androidx .appcompat .app .AppCompatActivity ;
4
4
5
+ import android .app .Activity ;
6
+ import android .app .AlertDialog ;
7
+ import android .app .DatePickerDialog ;
8
+ import android .app .Dialog ;
9
+ import android .app .TimePickerDialog ;
10
+ import android .content .Context ;
11
+ import android .content .DialogInterface ;
12
+ import android .content .Intent ;
13
+ import android .graphics .Color ;
14
+ import android .graphics .drawable .ColorDrawable ;
15
+ import android .os .Build ;
5
16
import android .os .Bundle ;
17
+ import android .util .DisplayMetrics ;
18
+ import android .view .View ;
19
+ import android .view .Window ;
20
+ import android .view .WindowManager ;
21
+ import android .widget .Button ;
22
+ import android .widget .DatePicker ;
23
+ import android .widget .ImageButton ;
24
+ import android .widget .LinearLayout ;
25
+ import android .widget .TextView ;
26
+ import android .widget .TimePicker ;
27
+ import android .widget .Toast ;
28
+
29
+ import java .util .Calendar ;
6
30
7
31
public class MainActivity extends AppCompatActivity {
32
+ Context context ;
33
+ private static final String LOG_TAG = "MainActivity => " ;
8
34
9
35
@ Override
10
36
protected void onCreate (Bundle savedInstanceState ) {
11
37
super .onCreate (savedInstanceState );
12
38
setContentView (R .layout .activity_main );
39
+ context = this ;
40
+ }
41
+
42
+
43
+
44
+ public void showNormalAlertDialog (View v ){
45
+ AlertDialog dialog = new AlertDialog .Builder (context )
46
+ .setTitle ("My Normal AlertDialog Title" )
47
+ .setMessage ("This is the normal Dialog message man" )
48
+ .setCancelable (false )
49
+ .setPositiveButton ("Positive-Btn" , new DialogInterface .OnClickListener () {
50
+ @ Override
51
+ public void onClick (DialogInterface dialogInterface , int i ) {
52
+ Toast .makeText (context , "You clicked positive button man" , Toast .LENGTH_SHORT ).show ();
53
+ }
54
+ })
55
+ .setNegativeButton ("Negative-Btn" , new DialogInterface .OnClickListener () {
56
+ @ Override
57
+ public void onClick (DialogInterface dialogInterface , int i ) {
58
+ Toast .makeText (context , "You Clicked Negative button man" , Toast .LENGTH_SHORT ).show ();
59
+ }
60
+ })
61
+ .show () ; // the show() method turns a AlertDialog.Builder to AlertDialog and returns it.
62
+
63
+
64
+ }
65
+
66
+ public void showCustomLayoutAlertDialog (View v ){
67
+ final Dialog dialog = new Dialog (context ) ;
68
+ dialog .setCancelable (false ); // dialog won't close if clicked outside dialog Window
69
+
70
+ dialog .requestWindowFeature (Window .FEATURE_NO_TITLE );
71
+ dialog .getWindow ().setBackgroundDrawable (new ColorDrawable (Color .TRANSPARENT ));
72
+ dialog .setContentView (R .layout .dialog_custom_dialog );
73
+
74
+ DisplayMetrics metrics = context .getResources ().getDisplayMetrics ();
75
+ int width = metrics .widthPixels ;
76
+ dialog .getWindow ().setLayout ((5 *width )/10 , WindowManager .LayoutParams .WRAP_CONTENT );
77
+
78
+
79
+ Button btn_Postitive = dialog .findViewById (R .id .dialogCustomDialog_Btn_Positive ) ;
80
+ Button btn_Negative = dialog .findViewById (R .id .dialogCustomDialog_Btn_Negative ) ;
81
+
82
+
83
+ btn_Negative .setOnClickListener (new View .OnClickListener () {
84
+ @ Override
85
+ public void onClick (View view ) {
86
+ Toast .makeText (context , "Negative button clicked" , Toast .LENGTH_SHORT ).show ();
87
+ dialog .dismiss ();
88
+ }
89
+ });
90
+
91
+ btn_Postitive .setOnClickListener (new View .OnClickListener () {
92
+ @ Override
93
+ public void onClick (View view ) {
94
+ Toast .makeText (context , "Positive button clicked" , Toast .LENGTH_SHORT ).show ();
95
+ dialog .dismiss ();
96
+ }
97
+ });
98
+ dialog .show ();
99
+
100
+ }
101
+
102
+
103
+
104
+
105
+
106
+
107
+ public void showDatePickerDialog (View v ){
108
+ Calendar c = Calendar .getInstance () ;
109
+ // These values become the pre-selected values when datePicker is opened
110
+ int currentYear = c .get (Calendar .YEAR );
111
+ int currentMonth = c .get (Calendar .MONTH );
112
+ int currentDay = c .get (Calendar .DAY_OF_MONTH );
113
+
114
+
115
+ DatePickerDialog datePickerDialog = new DatePickerDialog (context , new DatePickerDialog .OnDateSetListener () {
116
+ @ Override
117
+ public void onDateSet (DatePicker datePicker , int year , int month , int day ) {
118
+ int realMonth = month +1 ; //in stupid java library months starts from 0 instead from 1 ;
119
+ Toast .makeText (context , "Date selected is " + year + " - " + month + " - " + day , Toast .LENGTH_LONG ).show ();
120
+ }
121
+ }, 2017 , currentMonth , currentDay ); //used 2017 to show you the difference
122
+
123
+ Toast .makeText (context , "The year is 2017 intentionally" , Toast .LENGTH_SHORT ).show ();
124
+ datePickerDialog .show ();
125
+
126
+ }
127
+
128
+ public void showTimePickerDialog (View v ){
129
+ Calendar c = Calendar .getInstance () ;
130
+ int currentHour = c .get (Calendar .HOUR );
131
+ int currentMinute = c .get (Calendar .MINUTE );
132
+
133
+ TimePickerDialog timePickerDialog = new TimePickerDialog (context , new TimePickerDialog .OnTimeSetListener () {
134
+ @ Override
135
+ public void onTimeSet (TimePicker timePicker , int hour , int minute ) {
136
+ Toast .makeText (context , "Time selected is " + hour + ":" + minute , Toast .LENGTH_LONG ).show ();
137
+
138
+ }
139
+ }, currentHour , currentMinute , true ) ;
140
+
141
+ timePickerDialog .show ();
142
+
143
+ }
144
+
145
+ public void showDialogFragment (View v ){
146
+ DialogFragment_Hello dfHello = DialogFragment_Hello .getInstance ("Arijit Singh" ) ;
147
+ dfHello .show (getSupportFragmentManager (), "dfHelloTag" );
148
+
13
149
}
14
150
}
0 commit comments