Simple Timepicker For A Fragment Activity
I am trying to launch a timepicker from a edittext in a fragment Buffet_offerings_breakfast_menu2.java public class Buffet_offerings_breakfast_menu2 extends Fragment implements Pic
Solution 1:
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
http://developer.android.com/training/basics/fragments/communicating.html
You can interface as a call back to the activity. So get the time in activity and then communicate to fragment.
Example:
For the example sake i am extending Activity. You can extend FragmentAcitivty ans use support library and make proper imports.
publicclassMainActivityextendsActivityimplementsPickTime{
EditFragment newFragment;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newFragment = newEditFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container, newFragment);
//transaction.addToBackStack(null);
transaction.commit();
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
@OverridepublicvoidreturnTime(String value) {
// TODO Auto-generated method stubif(newFragment.isVisible())
{
newFragment.setEdittextvalue(value);
}
}
}
Fragment with just a Edittext
publicclassEditFragmentextendsFragment {
EditText ed;
@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragmentlayout,container,false);
return v;
}
@OverridepublicvoidonActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stubsuper.onActivityCreated(savedInstanceState);
ed = (EditText) getView().findViewById(R.id.editText1);
ed.setOnClickListener(newOnClickListener()
{
@OverridepublicvoidonClick(View v) {
// TODO Auto-generated method stubTimePickerFragment newFragment = newTimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
});
}
publicvoidsetEdittextvalue(String value) {
// TODO Auto-generated method stub
ed.setText(value);
}
}
TimerPickerFragment
publicclassTimePickerFragmentextendsDialogFragmentimplementsTimePickerDialog.OnTimeSetListener {
@OverridepublicvoidonAttach(Activity activity) {
// TODO Auto-generated method stubsuper.onAttach(activity);
mCallback = (PickTime) activity;
}
publicinterfacePickTime
{
publicvoidreturnTime(String value);
}
PickTime mCallback;
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the pickerfinalCalendarc= Calendar.getInstance();
inthour= c.get(Calendar.HOUR_OF_DAY);
intminute= c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return itreturnnewTimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
publicvoidonTimeSet(TimePicker view, int hourOfDay, int minute) {
// Do something with the time chosen by the userif(mCallback!=null)
{
StringBuildersb=newStringBuilder();
sb.append(hourOfDay);
sb.append(":");
sb.append(minute);
mCallback.returnTime(sb.toString());
}
}
}
Solution 2:
initialize mCallback in Buffet_offerings_breakfast_menu2 like this
@OverridepublicvoidonAttach(Activity activity) {
// TODO Auto-generated method stubsuper.onAttach(activity);
mCallback = (PickTime) activity;
}
Post a Comment for "Simple Timepicker For A Fragment Activity"