Created
April 14, 2015 11:36
-
-
Save eluleci/85a6948c6ffbcb620a51 to your computer and use it in GitHub Desktop.
Picking date with Fragment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.DatePickerDialog; | |
import android.app.Dialog; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v4.app.DialogFragment; | |
import java.util.Calendar; | |
public class DatePickerFragment extends DialogFragment { | |
@NonNull | |
@Override | |
public Dialog onCreateDialog(Bundle params) { | |
int year, month, day; | |
final Calendar c; | |
if (getArguments() != null) { | |
// set the date that is sent with params | |
c = (Calendar) getArguments().getSerializable("date"); | |
} else { | |
// set the date to the current date | |
c = Calendar.getInstance(); | |
} | |
year = c.get(Calendar.YEAR); | |
month = c.get(Calendar.MONTH); | |
day = c.get(Calendar.DAY_OF_MONTH); | |
try { | |
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getTargetFragment(), year, month, day); | |
} catch (ClassCastException cce) { | |
throw new RuntimeException("The fragment that uses the DatePickerFragment must implement DatePickerDialog.OnDateSetListener."); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EditProfileFragment extends Fragment implements DatePickerDialog.OnDateSetListener { | |
private final String TAG = "CompleteProfileInfoFragment"; | |
private Calendar birthDate; | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
View view = inflater.inflate(R.layout.fragment_complete_profile, container, false); | |
vBirthDate = (Button) view.findViewById(R.id.birthdate); | |
vBirthDate.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
showDatePickerDialog(); | |
} | |
}); | |
return view; | |
} | |
@Override | |
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { | |
birthDate = Calendar.getInstance(); | |
birthDate.set(Calendar.YEAR, year); | |
birthDate.set(Calendar.MONTH, monthOfYear); | |
birthDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); | |
vBirthDate.setText(new SimpleDateFormat("dd-MM-y").format(birthDate.getTime())); | |
vBirthDate.setHint(""); | |
} | |
public void showDatePickerDialog() { | |
DialogFragment newFragment = new DatePickerFragment(); | |
if (birthDate != null) { | |
Bundle bundle = new Bundle(); | |
bundle.putSerializable("date", birthDate); | |
newFragment.setArguments(bundle); | |
} | |
newFragment.setTargetFragment(this, 0); | |
newFragment.show(getActivity().getSupportFragmentManager(), "datePicker"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment