How do I create a pop-up Android Fragment that takes in a text input before returning to my main activity? -
for app, want user able press "change user" button, have fragment pop-up, take in text input, , save shared preferences upon exit, returning activity called from.
i methodology similar timepicker fragment have implemented, post @ end of this.
i using tutorial guide: dialog fragment tutorial
here dialog fragment class:
public class editnamedialog extends dialogfragment { private edittext medittext; public editnamedialog() { // empty constructor required dialogfragment } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_edit_name, container); getdialog().settitle("hello"); return view; } public boolean oneditoraction(textview v, int actionid, keyevent event) { return true; } }
here mainactivity calls it:
public class mainactivity extends activity implements timepickerfragment.fragmentcallbacks { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // use current time default values picker sharedpreferences sharedpref = this.getsharedpreferences("prediction", context.mode_private); final calendar c = calendar.getinstance(); int defaulthour = c.get(calendar.hour_of_day); int defaultminute = c.get(calendar.minute); int hour = sharedpref.getint("prediction_hour", defaulthour); int minute = sharedpref.getint("prediction_min", defaultminute); string timestring = integer.tostring(hour) + ":" + integer.tostring(minute); textview predictiontext = (textview) findviewbyid(r.id.prediction_time); predictiontext.settext(timestring); } public void showtimepickerdialog(view v) { dialogfragment newfragment = new timepickerfragment(); newfragment.show(getfragmentmanager(), "timepicker"); } public void showusernameedit(view c) { editnamedialog newfragment = new editnamedialog(); newfragment.show(getsupportfragmentmanager(), "editname"); } @override public void timeupdated(int hour, int minute) { toast.maketext(this, "hour: " + hour + " minute: " + minute, toast.length_short).show(); // finish(); // startactivity(new intent(this, mainactivity.class)); string timestring = integer.tostring(hour) + ":" + integer.tostring(minute); textview predictiontext = (textview) findviewbyid(r.id.prediction_time); predictiontext.settext(timestring); } }
i believe problem lies. either call show() getfragmentmanager() , says show() takes getsupportfragmentmanager(), or call getsupportfragmentmanager(), , says activity not have method. know extend activity fragmentactivity instead, creates other issues. call timepickerfragment, shown below:
public class timepickerfragment extends dialogfragment implements timepickerdialog.ontimesetlistener { @override public dialog oncreatedialog(bundle savedinstancestate) { // use current time default values picker context context = getactivity(); sharedpreferences sharedpref = context.getsharedpreferences( "prediction", context.mode_private); final calendar c = calendar.getinstance(); int defaulthour = c.get(calendar.hour_of_day); int defaultminute = c.get(calendar.minute); int hour = sharedpref.getint("prediction_hour", defaulthour); int minute = sharedpref.getint("prediction_min", defaultminute); // create new instance of timepickerdialog , return return new timepickerdialog(getactivity(), this, hour, minute, dateformat.is24hourformat(getactivity())); } public void ontimeset(timepicker view, int hourofday, int minute) { context context = getactivity(); sharedpreferences sharedpref = context.getsharedpreferences( "prediction", context.mode_private); sharedpreferences.editor editorh = sharedpref.edit(); editorh.putint("prediction_hour", hourofday); editorh.commit(); sharedpreferences.editor editorm = sharedpref.edit(); editorm.putint("prediction_min", minute); editorm.commit(); mcallbacks.timeupdated(hourofday, minute); } /** * interface */ private fragmentcallbacks mcallbacks; public interface fragmentcallbacks { void timeupdated(int hour, int minute); } @override public void onattach(activity activity) { super.onattach(activity); try { mcallbacks = (fragmentcallbacks) activity; } catch (classcastexception e) { throw new classcastexception( "activity must implement fragment two."); } } @override public void ondetach() { super.ondetach(); mcallbacks = null; } }
i suggest staying up-to-date latest support libraries, app uses latest in user interface designs (material design) , across devices app retain same look. suggest follows:
for dialogfragments, should pass arguments start new instance of fragment, instead of using shared preferences. while shared preferences still works, isn't ideal solution. best practices passing arguments dialog fragment shown below method called newinstance(). reason don't pass arguments constructor can found here
use appcompatdialog in oncreatedialog, , use imports shown below:
import android.support.v4.app.dialogfragment; import android.support.v7.app.alertdialog; import android.support.v7.app.appcompatdialog; ... ... public class timepickerfragment extends dialogfragment implements timepickerdialog.ontimesetlistener { public static timepickerfragment newinstance(int hour, int min){ timepickerfragment frag = new timepickerfragment(); bundle args = new bundle(); args.putint("hour", hour); args.putint("min", min); frag.setarguements(args); return frag; } @override public appcompatdialog oncreatedialog(bundle savedinstancestate) { // use current time default values picker final calendar c = calendar.getinstance(); int defaulthour = c.get(calendar.hour_of_day); int defaultminute = c.get(calendar.minute); int hour = getarguments().getint("hour"); int minute = getarguments().getint("min"); // create new instance of timepickerdialog , return return new timepickerdialog(getactivity(), this, hour, minute, dateformat.is24hourformat(getactivity())); } ... ... }
and when show following:
timepickerfragment.newinstance(hour, min).show(getsupportfragmentmanager(), "time_fragment");
and in activity should extend appcompatactivity follows:
public class mainactivity extends appcompatactivity implements timepickerfragment.fragmentcallbacks {
then in activity use getsupportfragmentmanager() when calling show();
if don't have latest support libraries downloaded app, add these dependencies gradle file:
dependencies { compile 'com.android.support:appcompat-v7:22.1.1' compile 'com.android.support:support-v4:22.1.1' ... }
Comments
Post a Comment