Skip to main content

Android DatePickerDialog

In this tutorial I want to explain about creating a  DatePickerDialog like,



In this I am having One Button and One EditText. On this example first it will load the current date to the EditText, While Tap on the button it will display the DatePickerDialog after tap on Done button DatePickerDialog it will update in the EditText.
 


activity_main.xml

This is a Layout of main page. This layout contain One Button and one EditText.



<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"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Date" />

    <EditText
         android:id="@+id/EdtTxtDate"
         android:layout_width="wrap_content"
          android:layout_height="wrap_content"
         android:ems="10"
         android:focusableInTouchMode="false"
         android:inputType="text" />

</LinearLayout>



MainActivity.java
 
public class MainActivity extends Activity {
       Button BtnSelectImage;
   private EditText EdtTxtDate;
   private static Context context;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

           context=MainActivity.this;
           EdtTxtDate = (EditText) findViewById(R.id.EdtTxtDate);

            String Date = getCurrentDate();
            EdtTxtDate.setText(Date);
       
        BtnSelectDate = (Button) findViewById(R.id.BtnSelectDate);
        BtnSelectDate.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO Auto-generated method stub
             
                              int monthOfYear, dayOfMonth, year;
                              String DateString = EdtTxtDate.getText().toString().trim();
                              if(DateString.equalsIgnoreCase("")){
                                  DateString = getCurrentDate();
                              }
                              dayOfMonth = Integer.parseInt(DateString.substring(0,DateString.indexOf("-")));
                              monthOfYear =    Integer.parseInt(DateString.substring(DateString.indexOf("-")+1,DateString.lastIndexOf("-")))-1;
                              year = Integer.parseInt(DateString.substring(DateString.lastIndexOf("-")+1));
                             
                              DatePickerDialog datePickerDialog;
                              datePickerDialog = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
                               
                                @Override
                                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                        int dayOfMonth) {
                                    monthOfYear = monthOfYear+1;
                                    String Month="", Day ="";
                                    if(monthOfYear<10)
                                        Month = "0"+ monthOfYear;
                                    else
                                        Month = monthOfYear+"";
   
                                    if(dayOfMonth<10)
                                        Day = "0"+ dayOfMonth;
                                    else
                                        Day = dayOfMonth+"";
                                   
                                    String AlertDate =  Day+"-"+Month+"-"+year;
                                    EdtTxtDate.setText(AlertDate);
                                }
                            }, year, monthOfYear, dayOfMonth);
                              datePickerDialog.setTitle("Select Date");
                              datePickerDialog.show();

            }
        });
       
    }

//Function to get current date
     public static String getCurrentDate() {
            String DATE_FORMAT_NOW = "dd-MM-yyyy";
            SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
            return sdf.format(new Date());
      }
   
}

Popular posts from this blog

Importing Image From Camera

In this tutorial I want to explain how to take a picture in camera ans import that picture to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will open Camera. After that the user can take photo After taking photo it will ask for save image. If the user save the image then it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<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"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_con…

How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.

In this blog I want to explain How to pass Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List with an intent.


Intent
                Intent is an object that is used to launch an activity or to return information  from an activity.  To start a new activity using startActivity(intent).  To get information from another activity we start activity using startActivityForResult(intent, requestCode).


               Here in  FromPage.java we are adding  Boolean, Int, String, Integer ArrayList, String ArrayList, int list and String List values to an intent. In ToPage.java We extract added values will retrieve from intent. 


FromPage.java

ArrayList<Integer>  IntArrayList = new  ArrayList<Integer>(Arrays.asList(100,13,18,22,44,13));
ArrayList<String>  StringArrayList = new  ArrayList<String>(Arrays.asList("String1", "String2", "String3", "String4"));

        int[] intList = {4, 6, 3, …

Importing Image From Gallery

In this tutorial I want to explain how to import images from Gallery to am imageview,

In this I am having One Button and One ImageView. While Tap on the button it will redirect to Gallery to Browse image. While we select an image it will display on the Imageview.

activity_main.xml

This is a Layout of main page. This layout contain One Button and one Imageview.



<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"
    android:gravity="top|center"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/BtnSelectImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Insert Image" />

    <ImageView
        …