Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to make a date spinner on Android
    primarykey
    data
    text
    <p>How do I create an Android spinner that open a date picker dialog?</p> <p>I want a Spinner that looks like the one in the calendar app, as shown in the screenshot below.</p> <p><img src="https://i.stack.imgur.com/aj4H7.png" alt="Screenshot of Android calendar app, with the desired GUI element highlighted"></p> <p>What I've already tried:</p> <p>I made a spinner in my layout XML and tried to set the onclick attribute to call a method that shows the dialog. When I put a method name in "on click", I got and error that says "The following classes could not be found: - Spinner (Change to android.widget.Spinner, Fix Build Path, Edit XML)".</p> <pre><code>&lt;Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="showDatePickerDialog" /&gt; </code></pre> <p>showDatePickerDialog is in the right class:</p> <pre><code>public void showDatePickerDialog(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getFragmentManager(), "Date"); } </code></pre> <p>And this is DatePickerFragment (copied from another website):</p> <pre><code>import java.util.Calendar; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.DialogFragment; import android.os.Bundle; import android.widget.DatePicker; public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker 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); } public void onDateSet(DatePicker view, int year, int month, int day) { // Do something with the date chosen by the user } } </code></pre> <p>SOLUTION:</p> <p>I found the solution at <a href="https://stackoverflow.com/questions/3928071/android-setting-a-spinner-onclicklistener?rq=1">another question</a>.</p> <p>I used a regular spinner in the layout file, then in my activity class, has this code:</p> <pre><code>dateSpinner = (Spinner)findViewById(R.id.spinner_date); View.OnTouchListener Spinner_OnTouch = new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { showDatePickerDialog(v); } return true; } }; View.OnKeyListener Spinner_OnKey = new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { showDatePickerDialog(v); return true; } else { return false; } } }; dateSpinner.setOnTouchListener(Spinner_OnTouch); dateSpinner.setOnKeyListener(Spinner_OnKey); </code></pre> <p>It seems a little hacked, but it works. Does anyone know a cleaner way to do it? It would not let me setOnClickListener() or setOnItemClickListener() on the spinner.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload