1 ) Activity Main
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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="com.example.lenovo.all_dialog_example.MainActivity" android:orientation="vertical"> <Button android:layout_width="368dp" android:layout_height="wrap_content" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="8dp" android:text="Button1" android:id="@+id/b1"/> <Button android:layout_width="368dp" android:layout_height="wrap_content" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="8dp" android:text="Button2" android:id="@+id/b2"/> <Button android:layout_width="368dp" android:layout_height="wrap_content" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="8dp" android:text="Button3" android:id="@+id/b3"/> <Button android:layout_width="368dp" android:layout_height="wrap_content" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="8dp" android:text="Button4" android:id="@+id/b4"/> <Button android:layout_width="368dp" android:layout_height="wrap_content" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="8dp" android:text="Button5" android:id="@+id/b5"/> <Button android:layout_width="368dp" android:layout_height="wrap_content" tools:layout_editor_absoluteY="0dp" tools:layout_editor_absoluteX="8dp" android:text="Button6" android:id="@+id/b6"/> </LinearLayout>
2 ) Alert Dialog Activity
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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="com.example.lenovo.all_dialog_example.alertdialogactivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This Is Alert Dialog Activity"/> </android.support.constraint.ConstraintLayout>3 ) Custom Dialog Activity<?xml version="1.0" encoding="utf-8"?><LinearLayout 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="com.example.lenovo.all_dialog_example.customdialogactivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="This Is Custom Dialog Activity"/> </LinearLayout>4 ) Activity Country Code RAW<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="4dp" > <!-- Views with default image and text, for demonstration. They will be changed in the adapter --> <ImageView android:layout_width="wrap_content" android:layout_height="35dp" android:id="@+id/flag" android:src="@mipmap/ic_launcher" android:scaleType="centerInside" android:layout_marginRight="8dp" android:layout_marginLeft="4dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Title" android:id="@+id/name" android:layout_gravity="center_vertical" /> </LinearLayout>5 ) Activity Date Picker<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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="com.example.lenovo.all_dialog_example.datepickeractivity"> </android.support.constraint.ConstraintLayout>6 ) List Dialog Activity<?xml version="1.0" encoding="utf-8"?><LinearLayout 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="com.example.lenovo.all_dialog_example.listdialogactivity" android:orientation="vertical"> <Button android:id="@+id/btnList" android:layout_height="wrap_content" android:layout_width="90dip" android:text="List" android:background="#00bfff" android:textColor="#fff" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="42dp" /> <Button android:id="@+id/btnCheckBox" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="CheckBox" android:background="#00bfff" android:textColor="#fff" android:layout_marginTop="12dp" android:layout_alignBaseline="@+id/btnRadio" android:layout_alignBottom="@+id/btnRadio" android:layout_alignLeft="@+id/btnRadio" android:layout_alignStart="@+id/btnRadio" /> <Button android:id="@+id/btnRadio" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="RadioButton" android:background="#00bfff" android:layout_marginTop="12dp" android:textColor="#fff" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </LinearLayout>7 ) Acticity Progress Dialog<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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="com.example.lenovo.all_dialog_example.progressdialogactivity"> </android.support.constraint.ConstraintLayout>8 ) Activity Date Time Picker<?xml version="1.0" encoding="utf-8"?><LinearLayout 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="com.example.lenovo.all_dialog_example.timepickeractivity" android:orientation="vertical"> <EditText android:layout_width="200dp" android:layout_height="wrap_content" android:id="@+id/in_time" android:layout_below="@+id/in_date" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SELECT TIME" android:id="@+id/btn_time" android:layout_below="@+id/btn_date" android:layout_alignLeft="@+id/btn_date" android:layout_alignStart="@+id/btn_date" /> </LinearLayout>9 ) Activity Custom Dialog<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.lenovo.all_dialog_example.customdialogactivity" tools:showIn="@layout/activity_customdialogactivity"> </android.support.constraint.ConstraintLayout>10 ) Activity Time Picker<?xml version="1.0" encoding="utf-8"?><android.support.constraint.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" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.lenovo.all_dialog_example.timepickeractivity" tools:showIn="@layout/activity_timepickeractivity"> </android.support.constraint.ConstraintLayout>11 ) Activity Custom Dialog<?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:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:layout_marginTop="10dp" android:src="@mipmap/ic_launcher_round"/> <TextView android:id="@+id/text" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toRightOf="@+id/image" android:text="IMMEHTA" android:textColor="@color/colorPrimaryDark" tools:textSize="25dp" />/> <Button android:id="@+id/dialogButtonOK" android:layout_width="100px" android:layout_height="wrap_content" android:text=" Ok " android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_below="@+id/image" /> </LinearLayout>12 ) Main Activitypublic class MainActivity extends AppCompatActivity { Button b1, b2, b3, b4, b5, b6; final Context context = this; private DatePicker datePicker; private Calendar calendar; private TextView dateView; private int year, month, day; static final int DATE_PICKER_ID = 1111; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); b1 = (Button) findViewById(R.id.b1); b2 = (Button) findViewById(R.id.b2); b3 = (Button) findViewById(R.id.b3); b4 = (Button) findViewById(R.id.b4); b5 = (Button) findViewById(R.id.b5); b6 = (Button) findViewById(R.id.b6); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { open(b1); } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("Arpan..."); // set the custom dialog components - text, image and button TextView text = (TextView) dialog.findViewById(R.id.text); text.setText("I Love You Arpan!"); ImageView image = (ImageView) dialog.findViewById(R.id.image); image.setImageResource(R.mipmap.ic_launcher); Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); dialogButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(getApplicationContext(),customdialogactivity.class); startActivity(intent); } }); dialog.show(); } }); b3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showDialog(DATE_PICKER_ID); } }); b4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(getApplicationContext(),listdialogactivity.class); startActivity(intent); } }); b5.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(getApplicationContext(),timepickeractivity.class); startActivity(intent); } }); b6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(getApplicationContext(),progressdialogactivity.class); startActivity(intent); } }); } public void open(View view) { AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setMessage("Are you sure, You wanted to make decision"); alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Intent intent=new Intent(getApplicationContext(),alertdialogactivity.class); startActivity(intent); } }); alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(),"You Click On No",Toast.LENGTH_SHORT).show(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_PICKER_ID: // open datepicker dialog. // set date picker for current date // add pickerListener listner to date picker return new DatePickerDialog(this, pickerListener, year, month,day); } return null; } private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() { // when dialog box is closed, below method will be called. @Override public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) { year = selectedYear; month = selectedMonth; day = selectedDay; // Show selected date b3.setText(new StringBuilder().append(month + 1) .append("-").append(day).append("-").append(year) .append(" ")); } }; }13 ) List Dialog Activitypublic class listdialogactivity extends AppCompatActivity implements View.OnClickListener { final int LIST_ALERTDIALOG = 0, CHECKBOX_ALERTDIALOG = 1, RADIOBUTTON_ALERTDIALOG = 2; final boolean checked_state[] = {false, false, false}; //The array that holds the checked state of the checkbox items final CharSequence[] day_check = {"Sunday", "Monday", "Tuesday"}; //items in the alertdialog that displays checkboxes final CharSequence[] day_list = {"Sunday", "Monday", "Monday"}; //items in the alertdialog that displays list final CharSequence[] day_radio = {"Sunday", "Monday", "Monday"}; //items in the alertdialog that displays radiobuttons private Button list, check, radio; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listdialogactivity); list = (Button) findViewById(R.id.btnList); check = (Button) findViewById(R.id.btnCheckBox); radio = (Button) findViewById(R.id.btnRadio); list.setOnClickListener(this); check.setOnClickListener(this); radio.setOnClickListener(this); } public Dialog onCreateDialog(int id) { switch (id) { case LIST_ALERTDIALOG: AlertDialog.Builder builder = new AlertDialog.Builder(listdialogactivity.this) .setTitle("Choose a Day") .setItems(day_list, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "The selected color is " + day_list[which], Toast.LENGTH_LONG).show(); } }); AlertDialog alertdialog = builder.create(); return alertdialog; case CHECKBOX_ALERTDIALOG: AlertDialog.Builder builder1 = new AlertDialog.Builder(listdialogactivity.this) .setTitle("Choose a Days") .setMultiChoiceItems(day_check, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { // TODO Auto-generated method stub //storing the checked state of the items in an array checked_state[which] = isChecked; } }) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub String display_checked_days = ""; for (int i = 0; i < 3; i++) { if (checked_state[i] == true) { display_checked_days = display_checked_days + " " + day_check[i]; } } Toast.makeText(listdialogactivity.this, "The selected day(s) is" + display_checked_days, Toast.LENGTH_LONG).show(); //clears the String used to store the displayed text display_checked_days = null; //clears the array used to store checked state for (int i = 0; i < checked_state.length; i++) { if (checked_state[i] == true) { checked_state[i] = false; } } //used to dismiss the dialog upon user selection. dialog.dismiss(); } }); AlertDialog alertdialog1 = builder1.create(); return alertdialog1; case RADIOBUTTON_ALERTDIALOG: AlertDialog.Builder builder2 = new AlertDialog.Builder(listdialogactivity.this) .setTitle("Choose a Day") .setSingleChoiceItems(day_radio, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "The selected Day is " + day_radio[which], Toast.LENGTH_LONG).show(); //dismissing the dialog when the user makes a selection. dialog.dismiss(); } }); AlertDialog alertdialog2 = builder2.create(); return alertdialog2; } return null; } @Override protected void onPrepareDialog(int id, Dialog dialog) { // TODO Auto-generated method stub switch (id) { case CHECKBOX_ALERTDIALOG: AlertDialog prepare_checkbox_dialog = (AlertDialog) dialog; ListView list_checkbox = prepare_checkbox_dialog.getListView(); for (int i = 0; i < list_checkbox.getCount(); i++) { list_checkbox.setItemChecked(i, false); } break; case RADIOBUTTON_ALERTDIALOG: AlertDialog prepare_radio_dialog = (AlertDialog) dialog; ListView list_radio = prepare_radio_dialog.getListView(); for (int i = 0; i < list_radio.getCount(); i++) { list_radio.setItemChecked(i, false); } break; } } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btnList: //triggering the List alertdialog showDialog(LIST_ALERTDIALOG); break; case R.id.btnCheckBox: //triggering the checkbox alertdialog showDialog(CHECKBOX_ALERTDIALOG); break; case R.id.btnRadio: //triggering the radio alertdialog showDialog(RADIOBUTTON_ALERTDIALOG); break; default: break; } } }14 ) Date Picker Activitypublic class datepickeractivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_datepickeractivity); } }15 ) Custom Dialog Activitypublic class customdialogactivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_customdialogactivity); } }16 ) Alert Dialog Activitypublic class alertdialogactivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alertdialogactivity); } }17 ) Progress Dialog Activitypublic class progressdialogactivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progressdialogactivity); } }18 ) Time Picker Activitypublic class timepickeractivity extends AppCompatActivity implements View.OnClickListener { Button btnTimePicker; EditText txtTime; private int mHour, mMinute; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_timepickeractivity); txtTime=(EditText)findViewById(R.id.in_time); btnTimePicker=(Button)findViewById(R.id.btn_time); btnTimePicker.setOnClickListener(this); } @Override public void onClick(View v) { final Calendar c = Calendar.getInstance(); mHour = c.get(Calendar.HOUR_OF_DAY); mMinute = c.get(Calendar.MINUTE); // Launch Time Picker Dialog TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { txtTime.setText(hourOfDay + ":" + minute); } }, mHour, mMinute, false); timePickerDialog.show(); } }Notes :1 ) Require Build Gaddlecompile 'com.android.support:appcompat-v7:25.3.1'compile 'com.android.support.constraint:constraint-layout:1.0.2'compile 'com.android.support:design:25.3.1'
Comments
Post a Comment