Type these code in main.Xml
<Button
android:background="#4B088A"
android:layout_width="60dp"
android:textSize="15dp"
android:layout_height="30dp"
android:textColor="#E6E6E6"
android:text="MONTH"
android:id="@+id/button1"
android:layout_x="25dip"
android:layout_y="56dip"></Button>
<Button
android:background="#4B088A"
android:layout_width="55dp"
android:textSize="20dp"
android:layout_height="30dp"
android:textColor="#E6E6E6"
android:text="TIME"
android:id="@+id/button2"
android:layout_x="225dip"
android:layout_y="58dip"></Button>
<TextView
android:layout_width="wrap_content"
android:id="@+id/textView1"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:textSize="15dp"
android:text=""
android:layout_x="108dip"
android:layout_y="56dip"></TextView>
---------------------------------------------
sr/package-name/sample.java
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class Sample extends Activity {
private TextView mDateDisplay;
private int mMonth;
private int mDay;
private int mHour;
private int mYear;
private int mMinute;
static final int TIME_DIALOG_ID = 0;
static final int DATE_DIALOG_ID = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDateDisplay = (TextView) findViewById(R.id.textView1);
Button pickDate = (Button) findViewById(R.id.button1);
pickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
Button pickTime = (Button) findViewById(R.id.button2);
pickTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(TIME_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
updateDisplay();
} @Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
case DATE_DIALOG_ID:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
} @Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case TIME_DIALOG_ID:
((TimePickerDialog) dialog).updateTime(mHour, mMinute);
break;
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
break;
}
} private void updateDisplay() {
mDateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append(" ")
.append(pad(mHour)).append(":")
.append(pad(mMinute)));
}
private DatePickerDialog.OnDateSetListener mDateSetListener =
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view,
int year,
int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mHour = hourOfDay;
mMinute = minute;
updateDisplay();
}
};
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
}
No comments:
Post a Comment