Updated 25 January 2016
A Dialog is a UI that is displayed in a window of its own. The dialog stays put until the user dismisses it with an action.
Dialogs are used to alert the user to some condition or to simply get the user to make a choice or input some data.
Before Honeycomb version, there was basic Dialog with its own showDialog / dismissDialog methods in Activity.
Honeycomb introduced Fragments to support reusing portions of UI and logic across multiple activities in an app. In parallel, the showDialog / dismissDialog methods in Activity are being deprecated in DialogFragments.
Before proceeding to DialogFragments, let us discuss Fragment:
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running. It is like a “subactivity” that you can reuse in different activities.
DialogFragments:
As a DialogFragment is also a Fragment that displays a dialog window, floating on top of its activity’s window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment’s state.
Let us create a basic example by taking activity in which we extends DialogFragment instead of normal Fragment:
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<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" > <Button android:id="@+id/show_dailog" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Show Dialog" android:background="@drawable/mybutton" android:textColor="#FFF" android:padding="10dp"/> </RelativeLayout> |
activity_my_dialog_fragment.xml
1 2 3 4 5 6 7 8 9 10 11 |
<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" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" /> </RelativeLayout> |
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package login.webkul.com.dialogfragmentexample; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends FragmentActivity { Button show_dailog; FragmentManager fragmentManager = getSupportFragmentManager(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show_dailog = (Button) findViewById(R.id.show_dailog); show_dailog.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { MyDialogFragment myDialogFragment = new MyDialogFragment(); myDialogFragment.show(fragmentManager, "DialogFragment"); } }); } } |
MyDailogFragment.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package login.webkul.com.dialogfragmentexample; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class MyDialogFragment extends DialogFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_my_dialog_fragment, container, false); getDialog().setTitle("About DialogFragment"); ((TextView)rootView.findViewById(R.id.about_fragment)).setText("As a DialogFragment is also a Fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state."); return rootView; } } |
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.