Android DialogFragment Example
In this tutorial, i will teach you how to implement DialogFragment.
A DialogFragment is a fragment that displays a dialog window which floats inactivity and it's shown depending on the activity state.
Let get Started.
Create a New Project in Android Studio.
Go to File>New>New Project
Enter Project name
Select Android Minimum SDK.This determines the devices which your application can run on.
Click Next and below window will open.Android Studio can create simple activities for you like Login and Maps Activity.
Click Empty Activity
Enter Activity name as the MainActivity.The main activity is the one that is run when the application starts.
Click Finish
A DialogFragment is a fragment that displays a dialog window which floats inactivity and it's shown depending on the activity state.
Let get Started.
Create a New Project in Android Studio.
Go to File>New>New Project
Enter Project name
Select Android Minimum SDK.This determines the devices which your application can run on.
Click Next and below window will open.Android Studio can create simple activities for you like Login and Maps Activity.
Click Empty Activity
Enter Activity name as the MainActivity.The main activity is the one that is run when the application starts.
Click Finish
MainActivity.java
package www.techoverload.net.dialogfragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.ViewGroup;
import android.view.View;
import android.view.LayoutInflater;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
Button dialogButton;
FragmentManager fm=getSupportFragmentManager();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dialogButton=(Button)findViewById(R.id.dfButton);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DFragment df=new DFragment();
df.show(fm,"Dialog Fragment");
}
});
}
}
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="www.techoverload.net.dialogfragment.MainActivity">
<Button
android:id="@+id/dfButton"
android:text="Dialog Fragment"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Create a Blank Fragment.
Enter Fragment name is DFragment
DFragment.java
package www.techoverload.net.dialogfragment;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater;
/**
* A simple {@link Fragment} subclass.
*/
public class DFragment extends DialogFragment{
public DFragment() {
// Required empty public constructor
}
@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>
Open Values>strings.xml and paste below code.
<resources>
<string name="app_name">DialogFragment</string>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>
AndroidManifest.xml
<?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>
Output: