How to create DialogFragment using Android Studio Tutorial

Using Android DialogFragment Tutorial

In this tutorial, you will learn how to create a DialogFragment.We will create two buttons that will show a dialog fragment and a custom AlertDialogFragment.


A Dialog window is normally displaced on top Fragment Activity.The fragments Dialog Object is shown depending on Fragments state.
Hiding, showing or dismissing the Dialog can be done through API.Implement of a DialogFragment should override 

public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
}
to supply its content .
Also using onCreateDialog(Bundle )  we can create a custom DialogFragment.

Let Get Started.

Open Android Studio them go to File>New Project.
Enter Project name as DialogFragmentExample.
Create Empty Activity and name it MainActivity.

Open MainActivity and copy below code.

package com.example.acer.dialogfragmentexample;

import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends FragmentActivity {
FragmentManager fm=getSupportFragmentManager();
Button alertDfrag,dialogFrag;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alertDfrag=(Button)findViewById(R.id.alertFragment);
dialogFrag=(Button)findViewById(R.id.dialogFragment);

alertDfrag.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
AlertFragment af=new AlertFragment();
af.show(fm,"Alert Fragment");


}
});

dialogFrag.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
DFragment df=new DFragment();
df.show(fm,"Dialog Fragment");
}
});



}
}


Go to res> layout and open activity_main.xml file.

NB:

When you create Activity or Fragment  in Android studio ,its layout is automatically created.The XML file defines the Activity Layout.
We are going to add two buttons in our activity_main.xml .
Copy and paste below code.

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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.acer.dialogfragmentexample.MainActivity">
<Button
android:id="@+id/dialogFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dialog Fragment"
android:layout_centerInParent="true"

/>

<Button
android:id="@+id/alertFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Alert Fragment"
android:layout_below="@id/dialogFragment"
android:layout_centerInParent="true"
/>


</RelativeLayout>


 Next, we are going to create Fragments.

Open Package com.example.acer.dialogfragmentexample; Right click on it 
Click new>Fragment>Fragmet Blank.
Dialog fragment in android studio


Below Dialog window will open.
android studio-creating fragment

Ensure you check the Create Layout XML so that once the fragment is created,its Layout will automatically be created.
Click Finish.

DFragment.java 


package com.example.acer.dialogfragmentexample;


import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
* A simple {@link Fragment} subclass.
*/
public class DFragment extends DialogFragment {





@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView=inflater.inflate(R.layout.fragment_d, container, false);
getDialog().setTitle("Dialog Example");
return rootView;

}

}


 fragment_d.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.acer.dialogfragmentexample.DFragment">

<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_centerInParent="true"
android:text="Welcome to Dialog Fragment" />

</RelativeLayout>


Create second Fragment  and save it as “AlertFragment”


AlertFragment.java


package com.example.acer.dialogfragmentexample;


import android.app.AlertDialog;
import android.os.Bundle;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v4.app.DialogFragment;


/**
* A simple {@link Fragment} subclass.
*/
public class AlertFragment extends DialogFragment {


public AlertFragment() {
// Required empty public constructor
}


public Dialog onCreateDialog(Bundle savedInstanceState)
{
return new AlertDialog.Builder(getActivity())
//setting Dialog Icon ..using the launcher icon
.setIcon(R.drawable.ic_launcher_background)
//setting dialog tittle
.setTitle("Alert Dialog Example")
//set message
.setMessage("Alert Dialog Tutorial")
//set positive button
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
})
//set negative Button
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
}).create();


}

}


NB:
AlertFragment does not have layout XML file because we have overridden onCreateView(Bundle) method.

String resources.
Open res>values>string.xml and paste below code.


<resources>
<string name="app_name">DialogFragmentExample</string>

<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>


Make no changes in Android Manifest file.
AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.acer.dialogfragmentexample">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>
</activity>
</application>

</manifest>

Run the application:

Output:
Dialog_fragment_example

Download Source Code.






COMMENTS

Name

android android Apps android emulators android.app antivirus arrays blogger bluestack Break Statement in Java constructors download downloader drivers emulators fragment java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql node.js php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: How to create DialogFragment using Android Studio Tutorial
How to create DialogFragment using Android Studio Tutorial
Using Android DialogFragment Tutorial
https://4.bp.blogspot.com/-GJ_EjAsix6U/WnTfO06hlDI/AAAAAAAAKGI/g95l5coj1Ck_aiA12X60j0VbfPG3-pgWQCEwYBhgL/s320/alert_dialog_fragment_example.PNG
https://4.bp.blogspot.com/-GJ_EjAsix6U/WnTfO06hlDI/AAAAAAAAKGI/g95l5coj1Ck_aiA12X60j0VbfPG3-pgWQCEwYBhgL/s72-c/alert_dialog_fragment_example.PNG
Java Codes
https://www.download-all.net/2018/02/how-to-create-dialogfragment-using.html
https://www.download-all.net/
https://www.download-all.net/
https://www.download-all.net/2018/02/how-to-create-dialogfragment-using.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy