Monday 21 September 2015

Datepicker Dialog in Android

Android DatePicker dialog allows the user to select a date in year, month and day in a standardized format. To create this, you can use Android.app.DatePickerDialog Class. Android provides a DatePickerDialog class which creates a floating dialog. In this tutorial we would be discussing how to create DatePickerDialog in android and how to deal with it.  DatePickerDialog class is inherited from the AlertDialog class. It has some additional features along with the features of an AlertDialog class. The DatePickerDialog when invoked allows the user to pick a date from the DatePicker widget.
The datePickerDialog will disable the Active Activity until the user selects the date and click “set” button of the dialog.
OnDateSetListener is the interface that has to be implemented with onDateSet callback to indicate the user is done filling in the date.
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"
   
android:orientation="vertical"
   
tools:context=".MainActivity">

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Choose Date"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="25dp" />

    <
TextView
       
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Date is :"
       
android:id="@+id/textView"
       
android:layout_marginTop="20dp" />
</
LinearLayout>
MainActivity.Java
package com.android.anil.datepickerdialog;



import android.app.DatePickerDialog;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.DatePicker;

import android.widget.TextView;



import java.util.Calendar;



public class MainActivity extends AppCompatActivity {

    Button cdatebtn;

    TextView displaydate;

    Calendar c=Calendar.getInstance();

    int cdate,cmonth,cyear;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        cdatebtn=(Button)findViewById(R.id.button);

        displaydate=(TextView)findViewById(R.id.textView);

        cdatebtn.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

     new DatePickerDialog(MainActivity.this,d1,c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)).show();

            }

        });

    }

    DatePickerDialog.OnDateSetListener d1=new DatePickerDialog.OnDateSetListener() {

        @Override

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

            cdate=dayOfMonth;

            cmonth=monthOfYear+1;

            cyear=year;

            displaydate.setText(cdate+"-"+cmonth+"-"+cyear);



        }

    };



}

Output :-
'





1 comment:

Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...