Skip to content

Commit f4b61b6

Browse files
committed
Added implementations of different types of dialogs
1 parent ed3c111 commit f4b61b6

File tree

9 files changed

+357
-14
lines changed

9 files changed

+357
-14
lines changed

Dialogs/.idea/gradle.xml

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package in.rafique.androidbasics.dialogs;
2+
3+
import android.content.Context;
4+
import android.os.Bundle;
5+
import android.util.DisplayMetrics;
6+
import android.view.LayoutInflater;
7+
import android.view.View;
8+
import android.view.ViewGroup;
9+
import android.view.WindowManager;
10+
import android.widget.Button;
11+
import android.widget.Toast;
12+
13+
import androidx.annotation.NonNull;
14+
import androidx.annotation.Nullable;
15+
import androidx.fragment.app.DialogFragment;
16+
17+
public class DialogFragment_Hello extends DialogFragment {
18+
Context context ;
19+
20+
public static DialogFragment_Hello getInstance(String someData){
21+
DialogFragment_Hello dialogFragment_hello = new DialogFragment_Hello() ;
22+
Bundle bundle = new Bundle() ;
23+
bundle.putString("data", someData);
24+
dialogFragment_hello.setArguments(bundle);
25+
return dialogFragment_hello ;
26+
}
27+
28+
@Nullable
29+
@Override
30+
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
31+
return inflater.inflate(R.layout.dialogfragment_hello, container, false) ;
32+
}
33+
34+
@Override
35+
public void onStart() {
36+
super.onStart();
37+
DisplayMetrics metrics = getResources().getDisplayMetrics();
38+
int width = metrics.widthPixels;
39+
//not doing this can certainly make your dialog faster, but can be really fuck up your layout
40+
// so better make sure that your custom layout needs dialogFragment and can't be done in normal customDialog
41+
getDialog().getWindow().setLayout((8*width)/10, WindowManager.LayoutParams.WRAP_CONTENT);
42+
// getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
43+
44+
}
45+
46+
@Override
47+
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
48+
super.onViewCreated(view, savedInstanceState);
49+
setCancelable(true);
50+
context = getActivity() ;
51+
52+
Button postiveButton = view.findViewById(R.id.dialogFragmentHello_Btn_Positive) ;
53+
Button negativeButton = view.findViewById(R.id.dialogFragmentHello_Btn_Negative) ;
54+
55+
postiveButton.setOnClickListener(new View.OnClickListener() {
56+
@Override
57+
public void onClick(View view) {
58+
Toast.makeText(context, "Positive button was nuked", Toast.LENGTH_SHORT).show();
59+
dismiss();
60+
}
61+
});
62+
63+
negativeButton.setOnClickListener(new View.OnClickListener() {
64+
@Override
65+
public void onClick(View view) {
66+
Toast.makeText(context, "Negative button was nuked", Toast.LENGTH_SHORT).show();
67+
dismiss();
68+
}
69+
});
70+
71+
}
72+
73+
74+
}

Dialogs/app/src/main/java/in/rafique/androidbasics/dialogs/MainActivity.java

+136
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,149 @@
22

33
import androidx.appcompat.app.AppCompatActivity;
44

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;
516
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;
630

731
public class MainActivity extends AppCompatActivity {
32+
Context context ;
33+
private static final String LOG_TAG = "MainActivity => " ;
834

935
@Override
1036
protected void onCreate(Bundle savedInstanceState) {
1137
super.onCreate(savedInstanceState);
1238
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+
13149
}
14150
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,53 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7+
android:orientation="vertical"
78
tools:context=".MainActivity">
89

910
<TextView
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
12-
android:text="Hello World!"
13-
app:layout_constraintBottom_toBottomOf="parent"
14-
app:layout_constraintLeft_toLeftOf="parent"
15-
app:layout_constraintRight_toRightOf="parent"
16-
app:layout_constraintTop_toTopOf="parent" />
13+
android:layout_gravity="center"
14+
android:text="Dialogs"
15+
android:layout_margin="16dp"
16+
/>
1717

18-
</androidx.constraintlayout.widget.ConstraintLayout>
18+
<Button
19+
android:layout_width="wrap_content"
20+
android:layout_height="wrap_content"
21+
android:text="Normal AlertDialog"
22+
android:onClick="showNormalAlertDialog"
23+
/>
24+
25+
<Button
26+
android:layout_width="wrap_content"
27+
android:layout_height="wrap_content"
28+
android:text="Custom Dialog"
29+
android:onClick="showCustomLayoutAlertDialog"
30+
/>
31+
32+
<Button
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:text="Datepicker Dialog"
36+
android:onClick="showDatePickerDialog"
37+
/>
38+
39+
<Button
40+
android:layout_width="wrap_content"
41+
android:layout_height="wrap_content"
42+
android:text="TimePicker Dialog"
43+
android:onClick="showTimePickerDialog"
44+
/>
45+
46+
<Button
47+
android:layout_width="wrap_content"
48+
android:layout_height="wrap_content"
49+
android:text="DialogFragment"
50+
android:onClick="showDialogFragment"
51+
/>
52+
53+
</LinearLayout>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical"
6+
android:padding="16dp"
7+
android:background="#ce00b2"
8+
>
9+
10+
<TextView
11+
android:layout_width="wrap_content"
12+
android:layout_height="wrap_content"
13+
android:text="This is custom dialog Title"
14+
android:layout_gravity="center"
15+
android:layout_marginBottom="16dp"
16+
/>
17+
18+
<TextView
19+
android:layout_width="wrap_content"
20+
android:layout_height="wrap_content"
21+
android:text="This is the custom dialog message man. How do you like this man"
22+
android:gravity="center|left"
23+
android:layout_marginLeft="16dp"
24+
android:layout_marginBottom="16dp"
25+
/>
26+
27+
<RelativeLayout
28+
android:layout_width="match_parent"
29+
android:layout_height="60dp"
30+
>
31+
32+
<Button
33+
android:layout_width="wrap_content"
34+
android:layout_height="wrap_content"
35+
android:id="@+id/dialogCustomDialog_Btn_Positive"
36+
android:text="Good Boy"
37+
android:layout_alignParentBottom="true"
38+
android:layout_alignParentRight="true"
39+
/>
40+
41+
<Button
42+
android:layout_width="wrap_content"
43+
android:layout_height="wrap_content"
44+
android:id="@+id/dialogCustomDialog_Btn_Negative"
45+
android:text="Bad Boy"
46+
android:layout_toLeftOf="@id/dialogCustomDialog_Btn_Positive"
47+
android:layout_marginRight="16dp"
48+
android:layout_alignParentBottom="true"
49+
/>
50+
51+
</RelativeLayout>
52+
</LinearLayout>

0 commit comments

Comments
 (0)