3

I am doing an Android Development and I have a doubt in date picker dialog.

When a button is clicked, a date picker pops up and the user should be able to select the date.

The code is successfully compiled, and when I click a button, the dialog pops up as shown in image 1, but I'd like the date picker to be as image 2.

Please let me know where do I have to change the code.

Below is the code:

  public class MainActivity extends AppCompatActivity {
Button B1;
int day_x, month_x, year_x;
static  final int id=0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    B1 = (Button)findViewById(R.id.button);
    B1.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            showDialog(id);
        }
    });
}
@Override
protected Dialog onCreateDialog(int id1)
{
    if (id1==id)
        return new DatePickerDialog(MainActivity.this,listener,year_x,month_x,day_x);
    return null;
}
protected DatePickerDialog.OnDateSetListener listener
        =new DatePickerDialog.OnDateSetListener()
{
    public void onDateSet(DatePicker view,int year,int month,int day)
    {
        year_x=year;
        month_x=month;
        day_x=day;
        Toast.makeText(MainActivity.this,Integer.toString(day_x)+":"+Integer.toString(month_x)+":"+
                Integer.toString(year_x),Toast.LENGTH_SHORT).show();
    }
     };
     }

image 1
image 2

Kalnode
  • 9,386
  • 3
  • 34
  • 62

3 Answers3

1

You can use AlertDialog.THEME_HOLO_LIGHT theme for that

DatePickerDialog dpd = new DatePickerDialog(context, 
  AlertDialog.THEME_HOLO_LIGHT, listener, year, month, day);

dpd.show();
Lorence Hernandez
  • 1,189
  • 12
  • 23
1

You can use this method but android.R.style.Theme_Holo_Light_Dialog_MinWidth is deprecated

public  void showDatePicker(Context context, DatePickerDialog.OnDateSetListener listener) {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dialog = new DatePickerDialog(context, android.R.style.Theme_Holo_Light_Dialog_MinWidth, listener, year, month, day);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.show();
}
user3678528
  • 1,741
  • 2
  • 18
  • 24
1

This is not a bug, the DatePickerDialog shows the calendar by default for android versions above Lollipop and shows the spinner for versions prior the Lollipop

If you want to use the traditional spinner for all versions, you can refer to multiple solutions in here

Zain
  • 37,492
  • 7
  • 60
  • 84