9

How can I achieve this layout for a time picker (and also for a date picker)? The default layout is either a (very big) clock or a (not beautiful) spinner style time picker. I saw this in several apps but couldn't find how to achieve this look and feel.

enter image description here

Amos
  • 1,321
  • 2
  • 23
  • 44
  • you can check time-picker in android here is some links just check http://www.tutorialspoint.com/android/android_timepicker_control.htm http://www.mkyong.com/android/android-time-picker-example/ http://androidexample.com/Time_Picker_With_AM_PM_Values_-_Android_Example/index.php?view=article_discription&aid=86&aaid=109 – Hanuman Apr 06 '15 at 12:18
  • http://www.tutorialspoint.com/android/android_timepicker_control.htm shows the time picker layout I want but it seems to be the default time picker though I don't see it like that in my preview (using latest api 22). I see it as a spinner with up/down buttons. – Amos Apr 06 '15 at 12:29

5 Answers5

8

For future reference for anyone that might need this, it is possible to achieve this time picker spinner by just choosing a TimePicker among the android widgets and then, on the xml file, type android:timePickerMode="spinner".

Your widget xml code should look like this:

<TimePicker
    android:id="@+id/timePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:timePickerMode="spinner" />

My answer was based on the documentation.

Rodrigo Borges
  • 116
  • 2
  • 4
6

Simplest way to achieve this in my view is since api level 21:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="8dp">

    <TimePicker
        android:id="@+id/timePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:timePickerMode="spinner" />

    <DatePicker
        android:id="@+id/datePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:calendarViewShown="false"
        android:datePickerMode="spinner" />

</LinearLayout>

You get this:

Date Time Picker Example

madfree
  • 141
  • 1
  • 7
1

This example has three pickers: NumberPicker, DatePicker and TimerPicker.

https://github.com/rodrigobusata/android-pickers

or

Use only the sample without the library and override for Android default pickers.

Busata
  • 1,029
  • 1
  • 13
  • 27
  • Thanks. What do you mean by "Use only the sample without the library and override for Android default pickers"? – Amos Apr 06 '15 at 12:23
  • Your Wellcome. In this sample there is a sample using a library that override the Android default pickers for can change the colors, but case you want only use a picker with the default colors, see only sample that is inside app folder and override "net.simonvt.timepicker.TimePicker" to "TimePicker". – Busata Apr 06 '15 at 12:32
  • As far as I understand, I need the net.simonvt.datepicker.DatePicker (for example) implementation in order to see the look and feel I want, where can I find it in order to use it in my code? in the link I only see a reference to it but not the actual implementation, what am I missing? – Amos Apr 06 '15 at 13:08
  • They are in sample next to "app" folder there are the folders "numberpicker-library", "datepicker-libray" and "timerpicker-library". – Busata Apr 06 '15 at 13:15
  • So I need to take the sources from here: https://github.com/rodrigobusata/android-pickers/tree/master/timerpicker-library/src/main/java/net/simonvt/timepicker and then I will be able to use them in the layout inside my app? – Amos Apr 06 '15 at 13:19
  • Weird, apparently the ugly spinner was only in the preview, when I actually run the app, it looks as I want it to be – Amos Apr 06 '15 at 13:41
  • This because in your preview may be greater than your emulator. – Busata Apr 06 '15 at 13:46
  • 1
    I think the link is no more – Faisal Naseer Jul 15 '16 at 06:47
0

You should note that this Dialog is a Fragment.

That said, the main idea will be this: When you need to launch this Date Picker, then you will show that fragment as an overlay of the current activity. This fragment has a date picked listener: an event that is launched when the user picks a date. Ok, everything easy until here. The hardest thing to understand will be the last one: Interaction between the fragment and the activity. What for? Because the Activity needs to know whick Date was selected. To achieve this the Fragment will have a public Interface that will be implemented by the activity.

Ok, let's see the code:

MyActivity.java

public class MyActivity extends FragmentActivity implements DatePickerFragment.FragmentInteraction{

    @Override
    public void onCreate(Bundle savedInstanceState){
        Button showDatePickerButton = (Button)findViewById(R.id.dateButton);
        showDatePickerButtonsetOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getSupportFragmentManager(), getString(R.string.datePickerTitle));
            }
        });
    }

    @Override
    public void doSomethingWithDate(int day, int month, int year) {
        Log.d("", "SELECTED DATE " + month + "/" + day  + "/" + year);
    }
}

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{

    private FragmentInteraction mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mListener = (FragmentInteraction) activity;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //Show picker with actual date presetted
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    @Override
    public void onDateSet(DatePicker view, int anio, int mes, int dia) {
        mListener.doSomethingWithDate(dia, mes + 1, anio);
    }

    public interface FragmentInteraction{
        public void doSomethingWithDate(int dia, int mes, int anio);
    }
}
Joaquin Iurchuk
  • 5,499
  • 2
  • 48
  • 64
  • Not sure I understand your reply. I want a time/date picker component to be placed on a fragment with other components where the user can select a date/time. My question was regarding the layout of the time picker component. – Amos Apr 06 '15 at 13:04
  • The just use the DatePickerFragment class only and skip the interface – Joaquin Iurchuk Apr 06 '15 at 13:05
  • So it can't be used as a simple object/component/view in a layout along with other objects/components/views? for example: a date picker, a textview and a button inside the same layout? – Amos Apr 06 '15 at 13:14
  • Yes, of course. But is not a simple component. It's a fragment component – Joaquin Iurchuk Apr 06 '15 at 13:15
0

Simply add timepickermode in xml like this <TimePicker android:id="@+id/time" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="10dp" android:gravity="center" android:timePickerMode="spinner" android:text="TimePicker" android:textSize="18sp" android:textColor="@android:color/black" android:textStyle="bold"/>