Dialog box in Android. Dialogs with fragment

Useful video on simple dialogs :
 https://youtu.be/tEfpO2R40_M

Useful video on creating Dialogs that survive Configuration changes :  https://youtu.be/d5OjQJajBAc

Useful Video to create Single Radio/checkbox choice Dialogs :
https://youtu.be/dtEdI_VC-Yo

There are Different Dialogs classes for different Dialogs which serve  Different Purposes.

The Most simple Dialog box is AlertDialog.


------------------------------------------------------------------------------------------------------

CREATE A SIMPLE ALERTDIALOG.


Step1] Create a Builder object from required Dialog Class ,In our Case its AlertDialog.Builder 

Implement the required method on the Builder to configure the Dialog.

setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog. Here setNeutralButton() is used to create a neutral cancel button.
All the setPostiveButton,setNegativeButton etc take 2 parameters. First is the Text of the Button and The second is the onClicklistener which triggers when we click the button.

setCancelable() method decides wheather to cancel the Dialog when user click on other parts of the screen or press the Back button. If you set it "False" , then the user cannot cancel the Dialog without pressing the Cancel Button that we prepare.

Step 2]  call show() method on the Builder to show the Dialog.


SIMPLE DIALOGS CANNOT SURVIVE CONFIGURATION CHANGES.



public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

//User clicks on Button to execute this method
public void showDialog(View view) {

final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Age Validation ")
.setMessage("Are you above 18 ? ")
.setCancelable(false);

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

Toast.makeText(MainActivity.this, "Good !", Toast.LENGTH_SHORT).show();
}
});

builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "Okay...", Toast.LENGTH_SHORT).show();
}
});

builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

//remove the Dialog box form screen.
//cancel button
builder.create().dismiss();

}
});

//show() is very very important .
builder.show();
}



------------------------------------------------------------------------------------------

CREATE DIALOGS THAT SURVIVE CONFIGURATION CHANGES.

Step 1] Create a seperate java class and make it extend to DialogFragment class , then implement its 

onCreateDialog() method.


Put DIALOG code inside this method and make this method return your builder object with create() implemented on it. Eg - builder.create().

Step 2] For Context , you can use , getContext() or getActivity() methods both work same.

NOTE : The DialogFragment class you create must be a public class or public static else it'll give an exception.

FragmentedDialog.java : 
public class FragmentedDialog extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

//Dialog code below

final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Age Validation ")
.setMessage("Are you above 18 ? ");

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

Toast.makeText(getContext(), "Good !", Toast.LENGTH_SHORT).show();
}
});

builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Okay...", Toast.LENGTH_SHORT).show();
}
});

builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

//remove the Dialog box form screen.
//cancel button
builder.create().dismiss();

}
});

//very very important
//ake it return your builder with create() implemented.
return builder.create();
}
}

Step 3]  In the MainActivity or whereever you want to show the Dialog , create the Object of your Dialogfragment class and implement show() method on that object.

The show method here will take 2 parameters , a fragmentmanager and other a String Tag.


NOTE : You dont need to mention the setCancelable() method in the Dialog code , you have to mention it in during initiation.


MainActivity.java : 
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

//User clicks on Button to execute this method
public void showDialog(View view) {

FragmentedDialog fragmentedDialog = new FragmentedDialog();

fragmentedDialog.setCancelable(false);

fragmentedDialog.show(getSupportFragmentManager(),"MyTag");

}
}

Now your Dialog can Survive Orientation Changes.


EXAMPLE 2 :


package com.deepesh.exampleapp4;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}


// User clicks a Button
public void DisplayDialogBox(View view) {

FragmentDialog1 fragmentDialog = new FragmentDialog1();
fragmentDialog.setCancelable(false);
fragmentDialog.show(getSupportFragmentManager(),"MYTAG");

}


public static class FragmentDialog1 extends DialogFragment{

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Age Validation");
builder.setMessage("Are you 18 ? ");

builder.setPositiveButton("YES", (dialog, which) -> {
Toast.makeText(getContext(), "YOU ARE 18", Toast.LENGTH_SHORT).show();
});

builder.setNegativeButton("NO", (dialog, which) -> {
Toast.makeText(getContext(), "YOU ARE NOT 18", Toast.LENGTH_SHORT).show();
});

builder.setNeutralButton("CANCEL", (dialog, which) -> {
builder.create().dismiss();
Toast.makeText(getContext(), "YOU CANCELLED", Toast.LENGTH_SHORT).show();
});

return builder.create();
}
}

}








-----------------------------------------------------------------------------------



























Comments

Popular posts from this blog

React Js + React-Redux (part-2)

React Js + CSS Styling + React Router (part-1)

ViteJS (Module Bundlers, Build Tools)