Android DialogFragment Example

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
Android DialogFragment Example


Select Android Minimum SDK.This determines the devices which your application can run on.
android-sdk

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.

creating-android-mainactivity-with-layout

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.

creating-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>

Download Source Code



Output:
dialog-fragment-example



COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: Android DialogFragment Example
Android DialogFragment Example
Android DialogFragment Example
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbzhik2WygvJHbhjt8mYtGHqKHbu-zd4RafGYK4EPyteoN2ODq5FcHC30edjwQzLmMWgHHTFjSuz-4dXUPCywiy553F2ACzy6IkpgZ8zFCRMQSp1gaO3pXV2IkhUbB611iV_5-PUm-YX_O/s320/android-dialog-fragment-example.PNG
https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbzhik2WygvJHbhjt8mYtGHqKHbu-zd4RafGYK4EPyteoN2ODq5FcHC30edjwQzLmMWgHHTFjSuz-4dXUPCywiy553F2ACzy6IkpgZ8zFCRMQSp1gaO3pXV2IkhUbB611iV_5-PUm-YX_O/s72-c/android-dialog-fragment-example.PNG
Code WHIZZ
https://code-whizz.blogspot.com/2018/02/android-dialogfragment-example.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2018/02/android-dialogfragment-example.html
true
5534598864497432585
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