Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to open Calendar on clicking the EditText and set the date.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 13.8k
    Comment on it

    To open Calendar on Clicking the EditText follow the steps mentioned below:-

    1) Create a Edit Text in your xml file.

    activity_main.xml

    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:tools="http://schemas.android.com/tools"
    3. android:layout_width="match_parent"
    4. android:layout_height="match_parent"
    5. tools:context=".MainActivity"
    6. android:orientation="vertical">
    7.  
    8. <EditText
    9. android:singleLine="true"
    10. android:id="@+id/dob_et"
    11. android:layout_marginTop="20dp"
    12. android:layout_marginRight="20dp"
    13. android:focusable="false"
    14. android:hint="Date of birth"
    15. android:inputType="date"
    16. android:layout_width="match_parent"
    17. android:layout_height="wrap_content" />
    18.  
    19. </LinearLayout>

    2) Now initialize  Edit Text in your java file. Here I have used the method DateDialog() to open the calendar popup and select the date.

    MainActivity.java

    1. public class MainActivity extends AppCompatActivity {
    2.  
    3. private Calendar mcalendar;
    4. private EditText mdob_et;
    5. private int day,month,year;
    6.  
    7. @Override
    8. protected void onCreate(Bundle savedInstanceState) {
    9. super.onCreate(savedInstanceState);
    10. setContentView(R.layout.activity_main);
    11.  
    12. mdob_et=(EditText)findViewById(R.id.dob_et);
    13. mdob_et.setOnClickListener(mClickListener);
    14. day=mcalendar.get(Calendar.DAY_OF_MONTH);
    15. year=mcalendar.get(Calendar.YEAR);
    16. month=mcalendar.get(Calendar.MONTH);
    17.  
    18. }
    19.  
    20. View.OnClickListener mClickListener=new View.OnClickListener() {
    21. @Override
    22. public void onClick(View v) {
    23.  
    24. DateDialog();
    25.  
    26. }
    27. };
    28.  
    29. public void DateDialog(){
    30.  
    31. DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {
    32.  
    33. @Override
    34. public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
    35. {
    36.  
    37. mdob_et.setText(dayOfMonth + "/" + monthOfYear + "/" + year);
    38.  
    39. }};
    40.  
    41. DatePickerDialog dpDialog=new DatePickerDialog(getActivity(), listener, year, month, day);
    42. dpDialog.show();
    43.  
    44. }
    45. }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: