Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DatePickerDialog called by a spinner onClick

I cant find anything on sof or google! so i need your help! id like to click on my (empty) spinner, so i get a date or timepicker dialog shown. I cant find anything how to handle that. these are my snippets for now.:

just my 2 objects:

DatePickerDialog dDialog = new DatePickerDialog(this, (OnDateSetListener) this, 2012, 3, 3);


        Spinner dSpinner = (Spinner) findViewById(R.id.spinner1);

xml:

 <Spinner
    android:id="@+id/spinner1"
    android:spinnerMode="dialog"
    android:prompt="@string/hint_date"
    android:layout_marginTop="40dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

maybe you have an idea how to handle that. I want to pick a date, call the picker, by onClick(what's not possible with spinners?!) I tried onclicklisteners as well.

like image 339
user1707493 Avatar asked Dec 06 '25 14:12

user1707493


2 Answers

The spinnerMode attribute is not what you're looking for. In order to show that DatePickerDialog when the user clicks the Spinner you'll have to create a custom Spinner widget and override the performClick method which is responsible for showing the default dialog with the Spinner's values. Below is an example:

public class CustomSpinnerDialog extends Spinner {

        // implement all the constructors   

    @Override
    public boolean performClick() {
        DatePickerDialog dDialog = new DatePickerDialog(getContext(),
                new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        Toast.makeText(getContext(), "Something",
                                Toast.LENGTH_SHORT).show();

                    }
                }, 2012, 3, 3);
        dDialog.show();
        return false;
    }

}

Then all you have to do is use this custom Spinner in your layout.

like image 136
user Avatar answered Dec 08 '25 02:12

user


Better way to do this is by masking a button to Spinner style as done in the Stock Contacts Application Edit Contact screen.

    <Button
        android:id="@+id/date_view"
        style="?android:attr/spinnerStyle"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingLeft="12dip"
        android:paddingStart="12dip" />
like image 45
Ankur Kumar Avatar answered Dec 08 '25 03:12

Ankur Kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!