Open top menu




In this tutorial implement the DatePicker ,explain below some code.

In this tutorial create the DatepickerDialog, the dialog is Override method

onCreateDialog(int id)

in this method set the date picker id



activity_main.xml

......................................................................................................................................................
<?xml version="1.0" encoding="utf-8"?>
<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" >

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Android Beginner tutorials"
        android:textSize="12dp" />

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
       
          >

        <EditText
            android:id="@+id/editText_dateinvoice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="15dp"
            android:ems="10"
         
            android:inputType="date"
            android:textStyle="italic" />

        <ImageButton
            android:id="@+id/imageButton_dateinvpice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_alignBottom="@+id/editText_dateinvoice"
             android:layout_toRightOf="@+id/editText_dateinvoice"
            android:src="@drawable/calendaer"
            android:contentDescription="@null" />

    </RelativeLayout>

</LinearLayout>



MainActivity.java
..................................................................................................................................................
package com.androidbeginnerpoint.testapplication;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;

public class MainActivity extends Activity implements OnClickListener {

private int day;
private int month;
private int year;
private ImageButton btn_ofinvoice;
private Calendar cal;
private EditText edtdate_ofinvoice;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn_ofinvoice=(ImageButton)findViewById(R.id.imageButton_dateinvpice);
edtdate_ofinvoice=(EditText)findViewById(R.id.editText_dateinvoice);
edtdate_ofinvoice.setEnabled(false); //disable edittext

cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);

btn_ofinvoice.setOnClickListener(this);
}

@Override
public void onClick(View v) {            
showDialog(0);
}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
return new DatePickerDialog(this, datePickerListener, year, month, day);
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
edtdate_ofinvoice.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
+ selectedYear);
}
};

}

0 comments