How to use DatePickerFragment inside PreferenceFragment to get Date

1.3k views Asked by At

I want to use DatePickerFragment inside PreferenceFragment for the date of birth. Can we use it like the way we use EditTextPreference or anything else we have to use. I know how DatePickerFragemnt and PreferenceFragment basic functionality.

Any help is appriciated.

Edited

.xml file for Preferences

    <PreferenceCategory android:title="@string/pref_profile_setting" >
        <EditTextPreference
                android:title="@string/pref_user_name"
                android:summary="@string/pref_user_name_summary"
                android:key="prefName"/>

        <com.mypkg.DatePickerFragment
            android:title="@string/pref_gender"
                android:summary="@string/pref_postcode_summary"
                android:key="prefDob"
                />
        <ListPreference
                android:title="@string/pref_gender"
                android:summary="@string/pref_postcode_summary"
                android:key="prefGender"
                android:entries="@array/gender"
                android:entryValues="@array/gender_type"/>

    </PreferenceCategory>
</PreferenceScreen>

Class that extended the PreferenceFragment

public class MyFragment extends PreferenceFragment implements OnPreferenceChangeListener{


    private EditTextPreference etNamePref;
    private ListPreference lvGenderPref;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_settings);

    }

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle paramBundle) {
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
        initView();

        return view;
    }


    private void initView() {
         etNamePref = (EditTextPreference)findPreference(ConstantsUtil.PREF_NAME);
         etNamePref.setOnPreferenceChangeListener(this);

    }


    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        String key = preference.getKey();
        if(key.equalsIgnoreCase(ConstantsUtil.PREF_NAME)){
            etNamePref.setSummary(newValue.toString());
        }
        return false;
    }
}
2

There are 2 answers

0
Naresh Sharma On BEST ANSWER

Add on Preference Tag in your .xml file

<PreferenceCategory android:title="@string/pref_profile_setting" >
        <EditTextPreference
                android:title="@string/pref_user_name"
                android:summary="@string/pref_user_name_summary"
                android:key="prefName"/>

<Preference android:title="@string/pref_select_dob"
             android:key="prefDob" >
        <ListPreference
                android:title="@string/pref_gender"
                android:summary="@string/pref_postcode_summary"
                android:key="prefGender"
                android:entries="@array/gender"
                android:entryValues="@array/gender_type"/>

    </PreferenceCategory>
</PreferenceScreen>

In .java class implement the OnPreferenceClickListener and below code

Preference preference = (Preference)findPreference("prefDob");
        preference.setOnPreferenceClickListener(this);

inside onClick call the DatePickerFragment like below

@Override
    public boolean onPreferenceClick(Preference preference) {
        String key = preference.getKey();
        if(key.equalsIgnoreCase("prefDob")){
            showDatePicker();
        }
        return false;
    }

    protected void showDatePicker() {

        DatePickerFragment date = new DatePickerFragment();

         /**
         * Set Up Current Date Into dialog
         */

        Calendar calender = Calendar.getInstance();
        Bundle args = new Bundle();
        args.putInt("year", calender.get(Calendar.YEAR));
        args.putInt("month", calender.get(Calendar.MONTH));
        args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
        date.setArguments(args);

         /**
         * Set Call back to capture selected date
         */

        date.setCallBack(ondate);
        date.show(getFragmentManager(), "Date Picker");

    }
    OnDateSetListener ondate = new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            String dob = String.valueOf(dayOfMonth) + "/" + String.valueOf(monthOfYear)+ "/" + String.valueOf(year);

        }
    };

Done !

5
Mostafa Gazar On

You will have to create your own custom DialogPreference for that, like the following

public class DatePickerPreference extends DialogPreference {


    public DatePickerPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DatePickerPreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        // TODO :: Set the initial value of the Preference.
    }

    @Override
    protected View onCreateDialogView() {
        return new DatePicker(getContext());
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        // TODO :: Save data to the SharedPreferences.
    }

}