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
Below Dialog window will open.
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: