Home > Android > DateTime in Android

DateTime in Android


First create XML file named it datetimepicker.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”
android:padding=”10px” >

<DatePicker
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_gravity=”center_horizontal” />

<TextView
android:id=”@+id/txtDateTime”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:textColor=”#00ff00″
android:textSize=”20dp” />

<Button
android:id=”@+id/btnDateTime”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Get change” />

</LinearLayout>

Create new activity named DateTimePicker.java add following code to DateTimePicker.java

package me.google.com;

import java.util.Calendar;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

public class DateTimePicker extends Activity implements OnClickListener {
TextView txtDateTime;
Button btnDateTime;
private int mDay;
private int mMonth;
private int mYear;
static final int DATE_DIALOG_ID = 0;

@Override
public void onCreate(Bundle instance) {
super.onCreate(instance);
setContentView(R.layout.datetimepicker);
txtDateTime = (TextView) findViewById(R.id.txtDateTime);
btnDateTime = (Button) findViewById(R.id.btnDateTime);
Calendar c = Calendar.getInstance();
mDay = c.get(Calendar.DAY_OF_MONTH);
mMonth = c.get(Calendar.MONTH);
mYear = c.get(Calendar.YEAR);
String strDateTime = “Today is: ” + String.valueOf(mDay) + “/”
+ String.valueOf(mMonth) + “/” + String.valueOf(mYear);
txtDateTime.setText(strDateTime);
btnDateTime.setOnClickListener(this);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog alert = new AlertDialog.Builder(this).create();
alert.setTitle(“Change Time”);
alert.setMessage(“Do you want to clear DateTime?”);
alert.setIcon(R.drawable.ic_launcher);
alert.setButton(“Yes”, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
}
});
alert.setButton2(“No”, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast tNo = Toast.makeText(getApplicationContext(),
“You pressed \”No\” Button”, 20);
tNo.show();
}
});
alert.show();
}

@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, datePickerListener, mYear,
mMonth, mDay);
}
return super.onCreateDialog(id);
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

// when dialog box is closed, below method will be called.
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
mYear = selectedYear;
mMonth = selectedMonth;
mDay = selectedDay;
txtDateTime.setText(new StringBuilder().append(mMonth + 1)
.append(“-“).append(mDay).append(“-“).append(mYear)
.append(” “));
}
};

}

Just so easy.

Categories: Android
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: