I'd like to create a control that allows a user to choose a day of the week (Monday) and a time of day (1:00 PM) in my Android activity. Not been able to find any good posts on this?
OK, I think I figured it out. I just don't love this solution because the Spinner I use for the day of the week doesn't match the "theme" of the timepicker, so it looks really bad.
EDIT: I changed the layout for the spinner you can see it at the bottom of the post, looks much better now.
DayTimePickerFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View thisDialog = inflater.inflate(R.layout.picker_day_time, container, false);
Button btnDateTimeOK = (Button)thisDialog.findViewById(R.id.btnDateTimeOK);
Button btnDateTimeCancel = (Button)thisDialog.findViewById(R.id.btnDateTimeCancel);
final Spinner spinSelectedDay = (Spinner)thisDialog.findViewById(R.id.spinSelectedDay);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_item_day_picker, getResources().getStringArray(R.array.days_of_week));
spinSelectedDay.setAdapter(adapter);
final TimePicker tpSelectedTime = (TimePicker)thisDialog.findViewById(R.id.tpSelectedTime);
btnDateTimeCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ActionBarActivity thisActivity = (ActionBarActivity) getActivity();
getDialog().dismiss();
}
});
btnDateTimeOK.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ActionBarActivity thisActivity = (ActionBarActivity) getActivity();
String strTime = null, strDay = null, strHour = null, strMinute = null, strAM_PM = null;
strDay = ((String)spinSelectedDay.getSelectedItem());
strMinute = tpSelectedTime.getCurrentMinute().toString();
if (tpSelectedTime.getCurrentMinute() < 10){
strMinute = "0"+strMinute;
}
if (tpSelectedTime.getCurrentHour() > 12){
strHour = (tpSelectedTime.getCurrentHour() - 12)+"";
strAM_PM = "PM";
}else if(tpSelectedTime.getCurrentHour() == 12){
strHour = tpSelectedTime.getCurrentHour().toString();
strAM_PM = "PM";
}else if(tpSelectedTime.getCurrentHour() < 12){
strHour = tpSelectedTime.getCurrentHour().toString();
strAM_PM = "AM";
}
if (strHour != null && strAM_PM != null){
strTime = strHour + ":" + strMinute + " " + strAM_PM;
}
etTarget.setText(strDay + " " + strTime);
getDialog().dismiss();
}
});
return thisDialog;
}
picker_day_time.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<Spinner android:id="@+id/spinSelectedDay" android:layout_width="fill_parent" android:layout_height="wrap_content">
</Spinner>
<TimePicker android:id="@+id/tpSelectedTime"
android:layout_width="wrap_content" android:layout_height="wrap_content" >
</TimePicker>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:id="@+id/btnDateTimeCancel" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1"
android:text="@string/button_text_cancel" android:onClick="DateTimeClickCancel" />
<Button android:id="@+id/btnDateTimeOK" android:layout_width="0dip" android:layout_height="wrap_content"
android:layout_weight="1" android:text="@string/button_text_ok" android:onClick="DateTimeClickOK" />
</LinearLayout>
</LinearLayout>
spinner_item_day_picker.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:text="Thursday"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textSize="@dimen/text_size_x_large" android:padding="10dp" android:gravity="center"/>
You could try to use some external library like android-betterpickers. It has, at least in my opinion, a great design:
https://github.com/derekbrameyer/android-betterpickers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With