We can also create another fragment of--dialogfragment. As the name suggests, Dialogfragment is a fragment floating above the activity. Dialogfragment comes in handy when the user's feedback is needed. Similar to using listfragment, you need to inherit the Dialogfragment base class.
The following will show you how to use Dialogfragment.
1. Create a project: Dialogfragmentexample.
2. Create a new class below the package path, Fragment1.
public class Fragment1 extends Dialogfragment {static Fragment1 newinstance (String title) {
Fragment1 fragment = new Fragment1 ();
Bundle args = new Bundle ();
Args.putstring ("title", title);
Fragment.setarguments (args);
return fragment; @Override public Dialog Oncreatedialog (Bundle savedinstancestate) {String title =
Getarguments (). getString ("title");
return new Alertdialog.builder (Getactivity ()). SetIcon (R.drawable.ic_launcher). Settitle (title) . Setpositivebutton ("OK", new Dialoginterface.onclicklistener () {public void OnClick (dialoginterface dialog, int whichbutton) {(Dialogfragmentex
ampleactivity) getactivity ()). Dopositiveclick (); }). SetnegAtivebutton ("Cancel", new Dialoginterface.onclicklistener () {public void OnClick (Di
Aloginterface dialog, int whichbutton) {(dialogfragmentexampleactivity)
Getactivity ()). Donegativeclick ();
). Create (); }
}
3. Code in the Dialogfragmentexampleactivity.java.
The public class Dialogfragmentexampleactivity extends activity {/** called the ' when the ' is ' the activity ' is a
-created
Verride public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Fragment1 dialogfragment = fragment1.newinstance (
"Are you sure to do this?");
Dialogfragment.show (Getfragmentmanager (), "dialog");
}
public void Dopositiveclick () {
//---Perform steps when user clicks on OK---
log.d ("Dialogfragmentexample", " User clicks on OK);
}
public void Donegativeclick () {
//---Perform steps when user clicks on Cancel---
log.d ("Dialogfragmentexample "," User clicks on Cancel ");
}