Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:37:59+00:00 2026-06-11T12:37:59+00:00

How do I create an Android spinner that open a date picker dialog? I

  • 0

How do I create an Android spinner that open a date picker dialog?

I want a Spinner that looks like the one in the calendar app, as shown in the screenshot below.

Screenshot of Android calendar app, with the desired GUI element highlighted

What I’ve already tried:

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)”.

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="showDatePickerDialog" />

showDatePickerDialog is in the right class:

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "Date");
}

And this is DatePickerFragment (copied from another website):

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
    }
}

SOLUTION:

I found the solution at another question.

I used a regular spinner in the layout file, then in my activity class, has this 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);

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:38:01+00:00Added an answer on June 11, 2026 at 12:38 pm

    I think in general what people do in this case is use a button and style it like a spinner (an approach here, I think a better approach, mostly just setting the style to android.R.attr.spinnerStyle), or they use a spinner but overload the click action to create a dialog.

    Of course the hard part there is creating the custom dialog that has nice month/day/year spin-ie-things (they aren’t spinners… they are kind of like slot machines or something) in them!

    There’s a few ways you can go about that, there is some google documentation here, and you can always just lift the android source code (although there are obviously drawbacks).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a spinner that looks exactly like the one in
I want to create an Android app that can be used on 2.3.3 all
I want to find out if it is possible to create Android API's that
I want to create an Android application that show the sales report from SQL
I want to create a android search application that can be used to search
I would like to create a custom spinner view so that the spinner options
I need to create a drop down list (spinner) in eclipse for Android whereby
To create facebook android native app we need to provide our Android application signature
I create an Android project for build my custom widget, and I want to
I want to use a Spinner that initially (when the user has not made

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.