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
    • 9.43k
    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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         tools:context=".MainActivity"
        android:orientation="vertical">
    
    <EditText
                android:singleLine="true"
                android:id="@+id/dob_et"
                android:layout_marginTop="20dp"
                android:layout_marginRight="20dp"
                android:focusable="false"
                android:hint="Date of birth"
                android:inputType="date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    
    </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

    public class MainActivity extends AppCompatActivity { 
    
         private Calendar mcalendar;     
         private EditText mdob_et;
         private int day,month,year;
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mdob_et=(EditText)findViewById(R.id.dob_et);
            mdob_et.setOnClickListener(mClickListener);
            day=mcalendar.get(Calendar.DAY_OF_MONTH);
            year=mcalendar.get(Calendar.YEAR);
            month=mcalendar.get(Calendar.MONTH);
    
    }
    
    View.OnClickListener mClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
        DateDialog();
    
        }
    };
    
     public void DateDialog(){
    
            DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {
    
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth)
                {
    
                    mdob_et.setText(dayOfMonth + "/" + monthOfYear + "/" + year);
    
                }};
    
         DatePickerDialog dpDialog=new DatePickerDialog(getActivity(), listener, year, month, day);
            dpDialog.show();
    
        }
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: