- Implementing the new Material Design Full-Screen Dialog for Android
- The final result
- When to use it
- Getting started
- Creating a DialogFragment
- Full Screen Dialog Fragment in Android
- Conclusion
- Get Solution Code
- Keep in touch
- koocbor / CreateDialog.java
- Implementing DialogFragment in Android
- What is DialogFragment?
- Three steps of creating custom Dialog
- Methods of DialogFragment
- Project Setup
- Creating Simple Dialog — Example
- Creating an Alert Dialog — Example
- Creating Dialog containing data(shared with Activity/Fragment)
- Conclusion
Implementing the new Material Design Full-Screen Dialog for Android
The Material Design guidelines describe a component called a full-screen dialog. This component is not yet officially available. In this blog post I describe how to easily create a full-screen dialog that complies with the guidelines by using the existing Android DialogFrament.
The final result
In this post I demonstrate how to achieve the following behavior:
One should notice the animations when opening and closing the dialog, the toolbar of the dialog with the close button and the positive action as text, and the full screen behavior of the dialog.
When to use it
According to the Material Design guidelines, a full-screen dialog should be used if the task meets any of the following criteria:
— Dialogs that include components which require keyboard input, such as form fields
— When changes aren’t saved instantly
— When components within the dialog open additional dialogs
The “x” in the toolbar indicates that changes are discarded when closing. The positive action (usually save or submit) is displayed as text in the toolbar.
Getting started
If you want to jump directly to the source code, you can also take a look at the Git repository.
We start with a new Android Studio project with an empty activity. I’ve changed the default theme so that everything is white, and added a button to my activity that should open our full-screen dialog later on.
Please note that I use the new AndroidX support library. All the stuff works with the old version as well, only the component names in the import statements and in the XML files have to be changed.
The app now looks like this:
Creating a DialogFragment
First we create a new class which inherits from DialogFragment. A dialog fragment offers the possibility to display a fragment as a modal. This dialog runs through the normal fragment lifecycle and is displayed with the help of a fragment manager. Therefore, this fragment also needs a layout. To achieve the desired result we need a toolbar with the close button and room for our actual content.
By using the attribute app:elevation = “0dp” of the AppBarLayout it is ensured that the toolbar has no shadow, but you can adjust this to your needs. The attributes android:paddingStart = “6dp” and android:paddingÉnd = “16dp” move the close button and the text for the positive action slightly away from the edges. The standard Material Design close icon is available here and serves as a navigation icon. As content the dialog contains only one TextView as an example, but here you can insert any views you like.
The corresponding class for this dialog fragment contains a static method to display the dialog. Moreover, it inflates our previously created layout in onCreateView. After the inflation the reference to the toolbar is retrieved. The title of the toolbar is set and the actions for the close button and for selecting the menu item are adjusted.
Источник
Full Screen Dialog Fragment in Android
In this post, we’ll learn about full screen dialog fragment Android. For better understanding, I will create a sample app that app contains full screen dialog in Android. Additionally, we’ll learn how to pass data dialog to fragment or also. So let’s get started.
Full Screen Dialog Fragment (Demo App)
1. Open android studio and create a new project
Create a new android project with Kotlin. Once project get synced up open style file from res=>value=>style.xml. Now create a style for app dialog named is DialogTheme
2. Let’s create a subclass of dialog fragment
Create a new Kotlin class named is FullScreenDialogExample. Here we are passing a CallbackListener inside constructor just for communicating for activity or fragment.
3. Open layout_full_screen_dialog layout file and put below code
This layout file we are taking one edit text and button. So in this full screen dialog, we will take input from edit text and send back to the parent (activity or fragment where you want )
4. Here is CallbackListener
Create new Interface named is CallbackListener
5. Open activity layout file and update below code
6. Show dialog using below code
7. Let’s put it into the main activity and see final code look like below
Conclusion
In this android example, We have learned the implementation of full screen dialog in android. I hope it’s helpful for you, then help me by sharing this post with all your friends.
Get Solution Code
Keep in touch
If you want to keep in touch and get an email when I write new blog posts, follow me on facebook or subscribe to us. It only takes about 10 seconds to register.
Источник
koocbor / CreateDialog.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
FullScreenDialog dialog = new FullScreenDialog (); |
FragmentTransaction ft = getSupportFragmentManager() . beginTransaction(); |
dialog . show(ft, FullScreenDialog . TAG ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
xml version = » 1.0 » encoding = » utf-8 » ?> |
RelativeLayout xmlns : android = » http://schemas.android.com/apk/res/android « |
android : layout_width = » match_parent « |
android : layout_height = » match_parent « |
> |
Content here —> |
RelativeLayout > |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
public class FullScreenDialog extends DialogFragment < |
public static final String TAG = » FullScreenDialog » ; |
@Override |
public void onCreate ( Bundle savedInstanceState ) < |
super . onCreate(savedInstanceState); |
setStyle( DialogFragment . STYLE_NORMAL , R . style . FullScreenDialog ); |
> |
@Override |
public void onStart () < |
super . onStart(); |
Dialog dialog = getDialog(); |
if (dialog != null ) < |
int width = ViewGroup . LayoutParams . MATCH_PARENT ; |
int height = ViewGroup . LayoutParams . MATCH_PARENT ; |
dialog . getWindow() . setLayout(width, height); |
> |
> |
@Override |
public View onCreateView ( LayoutInflater inflater , ViewGroup parent , Bundle state ) < |
super . onCreateView(inflater, parent, state); |
View view = getActivity() . getLayoutInflater() . inflate( R . layout . dialog_layout, parent, false ); |
return view; |
> |
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Источник
Implementing DialogFragment in Android
Dialogs are one of the most common and easiest ways of interactions with users. Almost every application has some dialogs present in it. It may be an alert dialog that is shown whenever you are logging out from some application or it can be any custom dialog that is used to take input from users or display some information to the user. We use dialogs to have a quick implementation of some feature in our application without creating an Activity.
For example, whenever you are connecting to a new wifi network then after selecting the wifi network, a Dialog will be opened and you can enter that password in the opened dialog and submit the details.
One of the popular implementations of Dialogs is AlertDialog. Alert Dialogs are used to alert the user before performing a particular task. For example, if you want to delete some images from the Gallery, then the Gallery app will alert you about the fact that by pressing the OK button which is the positive button, in this case, the image will be deleted permanently.
Dialogs are Awesome 🙂
So, in this blog, we will learn how to make Custom Dialogs using the DialogFragment in Android. So, let’s get started.
Here is the timeline for this blog:
- What is DialogFragment?
- Three steps of creating custom Dialog
- Methods of DialogFragment
- Project Setup
- Creating Simple Dialog — Example
- Creating an Alert Dialog — Example
- Creating Dialog containing data(shared with Activity/Fragment) — Example
- Conclusion
What is DialogFragment?
In a very simple sentence, a Dialog Fragment is a fragment that is used to make Dialogs that floats on some Activity.
DialogFragment is a utility class which extends the Fragment class. All the information regarding the Dialog or the data associated with the Dialog will be stored or managed in the Fragment only. You can use the DialogFragment in API level 11 or higher.
Since DialogFragment is associated with Fragment, so it has it’s own LifeCycle and now the Activity need not manage the lifecycle of Dialogs. Due to this reason, DialogFragments are recommended to be used while implementing Alert Dialog or any other type of Dialogs in Android and it is very easy to create a dialog fragment of our own because it requires only 3 steps. Let’s see those steps:
Three steps of creating custom Dialog
- First of all, you need to create a Kotlin/Java file for your Dialog Fragment. For example, CustomDialog.kt and this class will extend the DialogFragment() . Here in this class, all the methods related to dialogs will be there.
- After creating the class, you need to make the layout file of the dialog. According to your use-case, make the layout of your Dialog Fragment.
- And finally, you need to call your custom dialog from your Activity.
Methods of DialogFragment
There are certain methods that are associated with DialogFragment and we can use then in our Dialog Fragment class. Some of the most commonly used methods are:
- onAttach(): This is called when a fragment is first attached with its context.
- onCreate(): The initial creation of a fragment is done in onCreate and it is called after the onAttach.
- onCreateDialog(): This is used to build your custom dialog. It is mostly used for showing some AlertDialog(provided by Android).
- onCreateView(): This is used to supply the contents of the Dialog and this is entirely responsible for drawing the Dialog. It is generally used for creating custom dialogs. If you are creating an AlertDialog, then this method is not needed, only onCreateDialog is sufficient in that case.
- onViewCreated(): This is called when the Dialog is created. This is used to ensure that the view is created.
- onDestroy(): Thisis used to destroy the DialogFragement.
The order of execution of the above methods will be: onAttach -> onCreate -> onCreateDialog -> onCreateView -> onViewCreated -> onDestroy.
Project Setup
- Project Name: DialogFrament-Example
- Language Used: Kotlin
We will be sharing the data between the DialogFragment and the Activity and for this, we will be using Shared ViewModel. So, add the dependency of LiveData and ViewModel in you app level build.gradle file
Creating Simple Dialog — Example
In this example, we will be having a Dialog Fragment that will contain 2 text views(one for the title and other for subtitle) and 2 buttons (one for positive button and other for negative button). So, it is going to be a mock of AlertDialog.
Here, we will pass the title and subtitle text from the Activity. If you want to hardcode the title and subtitle then it is totally upon you.
So, First of all, add one button on the MainActivty so that whenever we click on that button the dialog will be opened.
Here is the code for activity_main.xml :
Now create a SimpleDialog class (under the root directory, right-click > New > Kotlin File/Class) and also, create a layout file named fragment_simple_dialog.xml .
The following is the code for fragment_simple_dialog.xml :
Here, we are having a title, a subtitle, and two buttons(for positive and negative response).
Add the below code in you SimpleDialog.kt file:
Here is the description of the above code:
- The newInstance() method is used to take the title and subtitle form the Activity.
- The onCreateView() method is responsible for creating the Dialog Fragment.
- In the onViewCreated() we are performing the tasks that need to be done after the creation of Dialog. For example, taking out the title and subtitle and setting it in the text view of the SimpleDialog.
- The setupView() is a user-defined function that is helping in setting the text in SimpleDialog.
- The setupClickListener() is an another user-defined function that is helping in setting all the click listeners of the SimpleDialog.
On clicking the positive/negative button, the dialog will be closed with the help of dismiss() method. You can change it according to your use-case.
Now, from the MainActivtiy, you need to open the Dialog on button click. So, following is the code for the same:
Here, I am passing the title and subtitle from the Activity.
Now, run your app and click on the button to see the dialog.
Creating an Alert Dialog — Example
If you want to use the AlertDialog provided by Android instead of your own custom dialog, then all you need to do is override the onCreateDialog method and create your AlertDialog there. In this case, you need not override the onCreateView method.
So, in the SimpleDialog.kt file, the following code will be there:
Creating Dialog containing data(shared with Activity/Fragment)
In this example, we will be having one EditText in the DialogFragment and a button. On clicking the button, the text in the EditText will be displayed on the MainActivtiy .
For this, we will use the concept of SharedViewModel. If you are not familiar with SharedViewModel, then you can read our blog on that topic from here.
NOTE: Don’t forget to add the dependency of LiveData and ViewModel.
Let’s first start with the MainActivity. In the activity_main.xml file, we need to add one button and one text view for displaying the name. So, the final code of the activity_main.xml will be:
Now, create a class named DialogWithData and SharedViewModel .
The following is the code for SharedViewModel.kt :
On the button click of the Dialog, the sendName() method will be called with the text in EditText and the name will be observed in the MainActivity.
The following is the code of DialogWithData.kt file:
On the click of the submit button, the sendName() method of the SharedViewModel is called.
Now, the name need to be observed in the MainActivity and also we need to implement the click listener of the button to open the DialogWithData fragment. So, the final code of MainActivtiy will be:
Now, you can run the application and enter some data in the EditText and see the text in MainActivity.
This is how you can share the data of the DialogFragment with the Activity. For further improvements, you can check if the values in the EditText is null or not. If it is null then you can show some error Toast and if it is not null then you can simply update the TextView.
This is all about DialogFragments.
Conclusion
In this blog, we learned how to implement DialogFragment in our Android Application. We saw that the traditional Alert Dialogs are no longer recommended. So, we need to use the DialogFragment to implement Dialogs. We did one example on DialogFragment to have a clear understanding of the same.
Источник