DatePicker View
Another view that is similar to the TimePicker is the DatePicker. Using the DatePicker, you can enable users to select a particular date on the activity.1 . main.xml file as shown here:
<?xml version=”1.0”encoding=”utf-8”?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<DatePicker
android:id=”@+id/datePicker”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
<TimePicker
android:id=”@+id/timePicker”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”/>
<Button
android:id=”@+id/btnSet”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Iamallset!”/>
</LinearLayout>
2 . Add in the following statements in bold to the MainActivity.java file:
package com.emergingandroidtech.DatePicker;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.widget.TimePicker;
import android.widget.DatePicker;
public class MainActivity extends Activity
{
TimePicker timePicker;
DatePicker datePicker;
int hour,minute;
static final int TIME_DIALOG_ID=0;
/**Called when the activity is first created.*/
@Override
public void onCreate(BundlesavedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//showDialog(TIME_DIALOG_ID);
timePicker=(TimePicker)findViewById(R.id.timePicker);
timePicker.setIs24HourView(true);
datePicker = (DatePicker) findViewById(R.id.datePicker);
//---Buttonview---
Button btnOpen=(Button)findViewById(R.id.btnSet);
btnOpen.setOnClickListener(newView.OnClickListener(){
public void onClick(View v)
{
Toast.makeText(getBaseContext(), “Date selected:” + datePicker.getMonth() + 1 + “/” + datePicker.getDayOfMonth() + “/” + datePicker.getYear() + “\n” + “Timeselected:”+timePicker.getCurrentHour()+ “:”+timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show(); }
});
}
@Override
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case TIME_DIALOG_ID:
return newTimePickerDialog( this,mTimeSetListener,hour,minute,false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener= newTimePickerDialog.OnTimeSetListener()
{
public void onTimeSet( TimePicker view,int hourOfDay,int minuteOfHour)
{
hour=hourOfDay; minute=minuteOfHour;
Toast.makeText(getBaseContext(), “Youhaveselected:“+hour+“:”+minute, Toast.LENGTH_SHORT).show();
}
};
}
How It Works
Like the TimePicker, you call the getMonth(), getDayOfMonth(), and getYear() methods to get the month, day, and year, respectively:
“Dateselected:”+datePicker.getMonth()+1+ “/”+datePicker.getDayOfMonth()+ “/”+datePicker.getYear()+“\n”+
* Note that the getMonth() method returns 0 for January, 1 for February, and so on. Hence, you need to add a one to the result of this method to get the month number.
No comments:
Post a Comment