android - Show dialogfragment inside a fragment -
i want display dialogfragment implements datepickerdialog inside fragment! have button listener displays datepickerdialog. button defined in fragment layout as:
<button android:id="@+id/buttonselectdate" android:layout_width="wrap_content" android:layout_alignparentright="true" android:layout_height="wrap_content" android:onclick="showdatepickerdialog" />
the code of fragment class following(my problem in showdatepickerdialog function):
public class schedulerchooserfragment extends fragment { private int year; private int month; private int day; textview datetext; view rootview; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { layoutinflater lf = getactivity().getlayoutinflater(); rootview = lf.inflate(r.layout.fragment_scheduler_chooser, container,false); return rootview; } // display current date public void showcurrentdateonview() { datetext = (textview) rootview.findviewbyid (r.id.textdate); final calendar c = calendar.getinstance(); year = c.get(calendar.year); month = c.get(calendar.month); day = c.get(calendar.day_of_month); system.out.println("year" + year); // set current date textview datetext.settext(month + 1 +"-"+day+"-"+year); // set current date datepicker } public void showdatepickerdialog(view v) { dialogfragment newfragment = new datepickerfragment(datetext); newfragment.show(getsupportfragmentmanager(), "datepicker"); } }
however, getsupportfragmentmanager not exist inside fragment. have tried getactivity().getsupportfragmentmanager app crashes. also, have tried getchildfragmentmanager, , app crashes too.
the datepickerfragment class folllowing:
import java.util.calendar; import android.app.datepickerdialog; import android.app.dialog; import android.os.bundle; import android.support.v4.app.dialogfragment; import android.widget.datepicker; import android.widget.textview; public class datepickerfragment extends dialogfragment implements datepickerdialog.ondatesetlistener{ textview txtdate; public datepickerfragment(textview txtdate) { super(); this.txtdate = txtdate; } @override public dialog oncreatedialog(bundle savedinstancestate) { // use current date default date in picker final calendar c = calendar.getinstance(); int year = c.get(calendar.year); int month = c.get(calendar.month); int day = c.get(calendar.day_of_month); // create new instance of datepickerdialog , return return new datepickerdialog(getactivity(), this, year, month, day); } public void ondateset(datepicker view, int year, int month, int day) { /*txtdate.settext(year+" " +month +" "+ date);*/ } }
i new android! sorry, if issue obvious.
thanks
in schedulerchooserfragment imports fragment , fragmentmanager must both android.support.v4.app
so,
import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager;
Comments
Post a Comment