Reputation: 1724
I would like to know if I can implement material design datepickerdialog for my android app
Code:
import java.util.Calendar;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.widget.DatePicker;
import android.widget.EditText;
public class SelectDateFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
EditText mEdit;
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
@Override
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
populateSetDate(yy, mm + 1, dd);
}
public void populateSetDate(int year, int month, int day) {
mEdit = (EditText) getActivity().findViewById(R.id.date);
mEdit.setText(day + "/" + month + "/" + year);
}
}
Simply, I dont want to use third part libraries.I just want to know if it can be done using android support libraries and how.
Upvotes: 2
Views: 16536
Reputation: 2263
https://github.com/wdullaer/MaterialDateTimePicker
a good Material Date Picker Library. Easy to use.
Just add compile'com.wdullaer:materialdatetimepicker:1.4.2' to your app: build.gradle
for example: Date Picker Dialog
// Show a datepicker when the dateButton is clicked
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
MainActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setThemeDark(modeDarkDate.isChecked());
dpd.vibrate(vibrateDate.isChecked());
dpd.show(getFragmentManager(), "Datepickerdialog");
}
});
and get your picked date from its implements method 'onDateSet' function
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = "You picked the following date: "+dayOfMonth+"/"+(++monthOfYear)+"/"+year;
dateTextView.setText(date);
}
Upvotes: 6
Reputation: 10829
You cannot have material design pickers in lower versions, the support library does not have these widgets. You will have to use 3rd party libraries. You can try this library, the author took the DateTimePicker
from Android Framework which you see in Android Lollipop
and compiled into a library and it supports from Android 4.0 on wards.
To add it in your app just add below lines in build.gradle:
dependencies {
compile 'com.wdullaer:materialdatetimepicker:1.4.1'
}
Find it on github link
The code taken from Android Framework can be found here
Upvotes: 3
Reputation: 801
Yes you can, but it would be a little troublesome to explain here. As a tip you could use a library such as this.
Although the Google recommends that you use the native Date picker from the os version. They claim that the user would be more familiarized with the proccess.
Best of luck.
Upvotes: 0