1. Are you ready for the Galaxy S20? Here is everything we know so far!

DatePicker Dialog issue

Discussion in 'Android Lounge' started by jeyshree, Feb 27, 2019.

  1. jeyshree

    jeyshree Lurker
    Thread Starter

    Hello Responsible,

    I am doing an Android Developement and i have a doubt in datebpicker dialog.
    When a button is clicked,a date picker pops up and user should be able to select the date.
    Below is the code written
    Code (Java):
    1. package com.example.jeyshree.datedialog;
    2.  
    3. import android.app.DatePickerDialog;
    4. import android.app.Dialog;
    5. import android.support.v7.app.AppCompatActivity;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.widget.Button;
    9. import android.widget.DatePicker;
    10. import android.widget.Toast;
    11.  
    12. public class MainActivity extends AppCompatActivity {
    13.     Button B1;
    14.     int day_x, month_x, year_x;
    15.     static  final int id=0;
    16.  
    17.     @Override
    18.     protected void onCreate(Bundle savedInstanceState) {
    19.         super.onCreate(savedInstanceState);
    20.         setContentView(R.layout.activity_main);
    21.         B1 = (Button)findViewById(R.id.button);
    22.         B1.setOnClickListener(new View.OnClickListener()
    23.         {
    24.             @Override
    25.             public void onClick(View v) {
    26.                 showDialog(id);
    27.             }
    28.         });
    29.     }
    30.     @Override
    31.     protected Dialog onCreateDialog(int id1)
    32.     {
    33.         if (id1==id)
    34.             return new DatePickerDialog(MainActivity.this,listener,year_x,month_x,day_x);
    35.         return null;
    36.     }
    37.     protected DatePickerDialog.OnDateSetListener listener
    38.             =new DatePickerDialog.OnDateSetListener()
    39.     {
    40.         public void onDateSet(DatePicker view,int year,int month,int day)
    41.         {
    42.             year_x=year;
    43.             month_x=month;
    44.             day_x=day;
    45.             Toast.makeText(MainActivity.this,Integer.toString(day_x)+":"+Integer.toString(month_x)+":"+
    46.                     Integer.toString(year_x),Toast.LENGTH_SHORT).show();
    47.         }
    48.     };
    49. }
    50.  
    The code is successfully compiled and when i click a button,the dialog pops as shown in image 1.But i want date picker as image 2

    Please let me know where do i have to change the code.
    Thanks in advance
     

    Attached Files:

    • 1.PNG
      1.PNG
      File size:
      21.7 KB
      Views:
      20
    • 2.PNG
      2.PNG
      File size:
      8 KB
      Views:
      18


  2. Deleted User

    Deleted User Guest

    You need to set spinner style for your Datepicker dialog. In the XML layout use this

    Code (Text):
    1.  
    2. <DatePicker    
    3.  android:id="@+id/datePicker"    
    4.  style="@style/MyDatePickerStyle"    
    5.  android:theme="@style/MyDatePickerSpinnerStyle"    
    6.   . . .
    7. />
    8.  
    And you may want to apply this style to prevent the calendar from being shown

    Code (Text):
    1.  
    2. <style name="MyDatePickerSpinnerStyle" parent="@android:style/Widget.Material.DatePicker">    
    3. <item name="android:datePickerMode">spinner</item>
    4.  <item name="android:calendarViewShown">false</item>
    5.  <item name="colorControlNormal">#d50000</item>
    6. </style>
    7.  
    Source: http://www.zoftino.com/android-datepicker-example
     
    MoodyBlues likes this.
Loading...

Share This Page

Loading...