Skip to content

Instantly share code, notes, and snippets.

@blehr
Created August 20, 2018 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blehr/4cc1a7329c2e5d544cadc17d81af15e0 to your computer and use it in GitHub Desktop.
Save blehr/4cc1a7329c2e5d544cadc17d81af15e0 to your computer and use it in GitHub Desktop.
package com.blehr.datepickerexample;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MyFragment extends Fragment {
EditText dateOfBirthET;
String selectedDate;
EditText ssnET;
public static final int REQUEST_CODE = 11; // Used to identify the result
private OnFragmentInteractionListener mListener;
public MyFragment() {
// Required empty public constructor
}
public static MyFragment newInstance() {
MyFragment fragment = new MyFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_my, container, false);
// get reference to edit text
dateOfBirthET = view.findViewById(R.id.dateOfBirthET);
// apply the input mask
dateOfBirthET.addTextChangedListener(new DateMask());
// get reference to edit text
ssnET = view.findViewById(R.id.ssnET);
// apply the input mask
ssnET.addTextChangedListener(new SsnMask());
// get fragment manager so we can launch from fragment
final FragmentManager fm = ((AppCompatActivity)getActivity()).getSupportFragmentManager();
// Using an onclick listener on the editText to show the datePicker
dateOfBirthET.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// create the datePickerFragment
AppCompatDialogFragment newFragment = new DatePickerFragment();
// set the targetFragment to receive the results, specifying the request code
newFragment.setTargetFragment(MyFragment.this, REQUEST_CODE);
// show the datePicker
newFragment.show(fm, "datePicker");
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// check for the results
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// get date from string
selectedDate = data.getStringExtra("selectedDate");
// set the value of the editText
dateOfBirthET.setText(selectedDate);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment