I am trying to use DatePicker in my Android app. When I add it to my layout, I get this message:
The following classes could not be instantiated:
- android.widget.DatePicker (Open Class, Show Error Log)
See the Error Log (Window > Show View) for more details.
So I looked in the error log, and this is what I saw:
android.widget.DatePicker failed to instantiate.
java.lang.NoClassDefFoundError: java/nio/charset/Charsets
at android.net.Uri.decode(Uri.java:1927)
at android.net.Uri$AbstractPart.getDecoded(Uri.java:1957)
at android.net.Uri$StringUri.getAuthority(Uri.java:579)
at android.provider.Settings$NameValueCache.getString(Settings.java:727)
at android.provider.Settings$System.getString(Settings.java:846)
at android.text.format.DateFormat.getDateFormatString(DateFormat.java:393)
at android.text.format.DateFormat.getDateFormatOrder(DateFormat.java:364)
at android.widget.DatePicker.reorderSpinners(DatePicker.java:511)
at android.widget.DatePicker.<init>(DatePicker.java:280)
at android.widget.DatePicker.<init>(DatePicker.java:145)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.instantiateClass(ProjectCallback.java:402)
at com.android.ide.eclipse.adt.internal.editors.layout.ProjectCallback.loadView(ProjectCallback.java:166)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:135)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:746)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:718)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:372)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:321)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:331)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:325)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:372)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1640)
at [SNIPPED FOR LENGTH; SEE REVISION HISTORY FOR FULL TRACE -ed]
Why is this happening?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="#+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (M-D-YYYY): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="#+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
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.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class MyAndroidAppActivity extends Activity {
private TextView tvDisplayDate;
private DatePicker dpResult;
private Button btnChangeDate;
private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 999;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCurrentDateOnView();
addListenerOnButton();
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
dpResult = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// set current date into datepicker
dpResult.init(year, month, day, null);
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,
year, month,day);
}
return null;
}
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) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
}
Read this code it might be helpful to you
Related
Am building an demo app, in which I would like to allow people who are above 21yrs, how do I do this.
I want to take there DOB and calculate the age, with the current year. I want to use DatePicker, but messed up seeing different snippets. thanks
This is a simple code that will guide your to achieve what your are planning for.
It uses a button click to launch the DatePicker Dialog
Layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.inducesmile.mydatepicker.MainActivity">
<EditText
android:id="#+id/display_dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"/>
<Button
android:id="#+id/select_dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:textColor="#000000"
android:textSize="15sp"
android:text="Select your Date of birth"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
Then the Activity class
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private DatePickerFragment datePickerFragment;
private static Calendar dateTime = Calendar.getInstance();
protected static int mYear;
protected static int mMonth;
protected static int mDay;
private static final int allowedDOB = 21;
protected static EditText displayDOB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayDOB = (EditText)findViewById(R.id.display_dob);
Button dobButton = (Button) findViewById(R.id.select_dob);
dobButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
datePickerFragment = new DatePickerFragment();
datePickerFragment.show(getSupportFragmentManager(), "Select Your Birthday");
}
});
}
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Using current date as start Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Get DatePicker Dialog
return new DatePickerDialog(getActivity(), this, mYear, mMonth, mDay);
}
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
dateTime.set(mYear, monthOfYear, dayOfMonth);
long selectDateInMilliSeconds = dateTime.getTimeInMillis();
Calendar currentDate = Calendar.getInstance();
long currentDateInMilliSeconds = currentDate.getTimeInMillis();
SimpleDateFormat simpleDate = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
String strDt = simpleDate.format(dateTime.getTime());
displayDOB.setText(strDt);
if (selectDateInMilliSeconds > currentDateInMilliSeconds) {
Toast.makeText(getActivity(), "Your birthday date must come before taday's date", Toast.LENGTH_LONG).show();
return;
}
long diffDate = currentDateInMilliSeconds - selectDateInMilliSeconds;
Calendar yourAge = Calendar.getInstance();
yourAge.setTimeInMillis(diffDate);
long returnedYear = yourAge.get(Calendar.YEAR) - 1970;
if (returnedYear < allowedDOB) {
Toast.makeText(getActivity(), "Sorry!!! You are not allowed to use this app", Toast.LENGTH_LONG).show();
return;
} else {
// move to another activity page
}
}
}
}
if you want to limit the age from datepicker itself you can do like this,
<com.deevita.patient.Custom_Layouts.editText
android:id="#+id/dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:drawableStart="#drawable/ic_date_icon"
android:drawablePadding="#dimen/drawable_padding"
android:focusable="false"
android:hint="#string/birthday"
android:inputType="date"
android:padding="#dimen/padding_common_new"
android:textSize="#dimen/font_small">
</com.deevita.patient.Custom_Layouts.editText>
add the following code in java
dob.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR,-21); // the age you want to limit
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker = new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
String date = String.valueOf(dayOfMonth) + "-" + String.valueOf(monthOfYear + 1)
+ "-" + String.valueOf(year);
dob.setText(date);
}
}, yy, mm, dd);
datePicker.getDatePicker().setMaxDate(calendar.getTimeInMillis());
datePicker.show();
}
});
if user entered age
driverage = edtdriverage.getText().toString();
else if ( Integer.parseInt(driverage)> 55 || Integer.parseInt(driverage)< 18) {
GlobalMethods.Toast(AddDriverActivity.this, getString(R.string.enter_driver_age_min));
cancel = true;
}
you can achieve by validation method also
How do I disable side by side DatePicker shown in my activity (running under KitKat/ Android 4.4, I'm using Sony Xperia to run it), I've been struggling so much finding this one, the left side contains scrolled part and the right side contains full calender, I want only the full calender shown:
import java.util.Calendar;
import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TextView;
public class versi2 extends Activity {
private TextView textViewTanggal;
private DatePicker datePicker;
private int year;
private int month;
private int day;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.versi2);
setCurrentDateOnView();
}
// display current date
public void setCurrentDateOnView() {
textViewTanggal = (TextView) findViewById(R.id.tvDisplayDate);
datePicker = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// set current date into textview
textViewTanggal.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
datePicker.init(year, month, day, dateSetListener);
}
private DatePicker.OnDateChangedListener dateSetListener = new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
textViewTanggal.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(dayOfMonth + 1).append("-").append(monthOfYear).append("-")
.append(year).append(" "));
}
};
}
And layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
....
....
<DatePicker
android:id="#+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
DatePicker has a datePickerMode attribute.
Use android:datePickerMode="calendar" to show just the calendar.
Ok so i have this app i'm working on... It has a left menu slide-out using ABS and the sliding menu lib. Here's my fragment that is launched once the menu item is selected. This fragment is suppose to have a text field to display the date you select and a button to select a date. Any idea's on what i'm doing wrong that SelectDateFragment is erroring?
Here's the XML layout:
<EditText
android:id="#+id/dateselected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="56dp"
android:ems="10"
android:inputType="date" >
<requestFocus />
</EditText>
<Button
android:id="#+id/pickdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/dateselected"
android:layout_toRightOf="#+id/dateselected"
android:contentDescription="#string/selectdate"
android:cropToPadding="true"
android:onClick="selectDate"
android:src="#drawable/ic_datepicker" />
</RelativeLayout>
Here's the java class:
import java.util.Calendar;
import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.support.v4.app.DialogFragment;
import android.widget.EditText;
public class DatePicker extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaceState){
return inflater.inflate(R.layout.fragment_charting, container, false);
}
EditText mEdit;
#Override
public void onStart() {
super.onStart();
}
public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getFragmentManager(), "DatePicker");
}
public void populateSetDate(int year, int month, int day) {
mEdit = (EditText)getView().findViewById(R.id.dateselected);
mEdit.setText(month+"/"+day+"/"+year);
}
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
#Override
public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// TODO Auto-generated method stub
populateSetDate(year, monthOfYear, dayOfMonth);
}
}
}
EDIT:
I'm trying to follow http://javapapers.com/android/android-datepicker/
The error i'm getting is the fragment inner class should be static. I don't understand enough about this topic, and from reading more i still do not, to understand why his works and mine does not.
try this one
shipdate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(999);
}
});
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 999:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener, year, month,
day);
}
return null;
}
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) {
final Calendar cf = Calendar.getInstance();
year = cf.get(Calendar.YEAR);
month = cf.get(Calendar.MONTH);
day = cf.get(Calendar.DAY_OF_MONTH);
// set selected date into textview
shipdate.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
In Android DatePicker, in some OS it shows Month as "Jan, Feb, Mar...Dec", on other it shows as 1,2,3..12
Is there a way to make it consistent througthout so that it should display 1,2,3.. 12 always as month?
Reason for displaying 1,2,3..12 instead of string is the localization support for various kind of languages.
Also is it advisable to do so?
Thanks.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnChangeDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Date" />
<TextView
android:id="#+id/lblDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date (April-10-2012): "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<DatePicker
android:id="#+id/dpResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Date picker class
import java.util.Calendar;
import java.util.Locale;
import com.datepicker.R.string;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class DatepickerActivity extends Activity {
private TextView tvDisplayDate;
private DatePicker dpResult;
private Button btnChangeDate;
private int year;
private int month;
private int day;
private String str;
public static long UTC (int year, int month, int day, int hour, int minute, int second) {
return 1;
}
static final int DATE_DIALOG_ID = 999;
//private static final int August =4;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setCurrentDateOnView();
addListenerOnButton();
}
// display current date
public void setCurrentDateOnView() {
tvDisplayDate = (TextView) findViewById(R.id.tvDate);
dpResult = (DatePicker) findViewById(R.id.dpResult);
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar. MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
// str=c.getDisplayName(c.MONTH, 2, Locale.US);
// set current date into textview
tvDisplayDate.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// set current date into datepicker
dpResult.init(year, month, day, null);
}
public void addListenerOnButton() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
// set date picker as current date
return new DatePickerDialog(this, datePickerListener,year, month,day);
}
return null;
}
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) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// set selected date into textview
tvDisplayDate.setText(new StringBuilder().append(str + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
// set selected date into datepicker also
dpResult.init(year, month, day, null);
}
};
}
I'm a beginning android developer.
I need to pass data from two date pickers and a spinner to another activity.
Can some one help me with this code:
package qi.com;
import java.text.DateFormat;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
//multi date input
public class CoaActivity extends Activity implements AdapterView.OnItemSelectedListener {
TextView selection;
TextView mDateStart;
TextView mDateEnd;
Button mPickStart;
Button mPickEnd;
Calendar startDate;
Calendar EndDate;
int mYear;
int mMonth;
int mDay;
//list menu
String[] items = {"Pendapatan","Biaya"};
static final int DATE_DIALOG_ID = 0;
private TextView activeDateDisplay;
private Calendar activeDate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//display to text view from list View
selection = (TextView) findViewById(R.id.selection);
Spinner spin= (Spinner) findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter<String> aa= new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, items);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa);
//capture view elements
mDateStart = (TextView) findViewById(R.id.DateStar);
mPickStart = (Button) findViewById(R.id.btn_PickDateStart);
//current date
startDate = Calendar.getInstance();
//listener click button
mPickStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(mDateStart, startDate);
}
});
//END Date
mDateEnd = (TextView) findViewById(R.id.DateEnd);
mPickEnd = (Button) findViewById(R.id.btn_PickDateEnd);
EndDate = Calendar.getInstance();
//listener click button
mPickEnd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDateDialog(mDateEnd, EndDate);
}
});
//display current date
updateDisplay(mDateStart, startDate);
updateDisplay(mDateEnd, EndDate);
//Detail("tes");
}
private void updateDisplay(TextView dateDisplay, Calendar date) {
dateDisplay.setText(
new StringBuilder()
// Month is 0 based so add 1
//.append(date.get(Calendar.DAY_OF_MONTH)).append("-")
.append(date.get(Calendar.DATE)).append("-")
.append(date.get(Calendar.MONTH) + 1).append("-")
.append(date.get(Calendar.YEAR)).append(" "));
//.append(DateFormat.getDateInstance(Calendar.MONTH)));
}
public void showDateDialog(TextView dateDisplay, Calendar date) {
activeDateDisplay = dateDisplay;
activeDate = date;
showDialog(DATE_DIALOG_ID);
}
private OnDateSetListener dateSetListener = new OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
activeDate.set(Calendar.YEAR, year);
activeDate.set(Calendar.MONTH, monthOfYear);
//activeDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
activeDate.set(Calendar.DATE, dayOfMonth);
updateDisplay(activeDateDisplay, activeDate);
unregisterDateDisplay();
}
};
private void unregisterDateDisplay() {
activeDateDisplay = null;
activeDate = null;
}
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, dateSetListener, activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DATE));
}
return null;
}
protected void onPrepareDialog(int id, Dialog dialog) {
super.onPrepareDialog(id, dialog);
switch (id) {
case DATE_DIALOG_ID:
((DatePickerDialog) dialog).updateDate(activeDate.get(Calendar.YEAR), activeDate.get(Calendar.MONTH), activeDate.get(Calendar.DATE));
break;
}
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
selection.setText(items[position]);
Intent grid=new Intent(this, Grid.class);
this.startActivity(grid);
//send to another activity
//Detail(items[position]);
}
public void onNothingSelected(AdapterView<?> parent) {
selection.setText("");
}
//another activity
public void Detail(String id)
//public void Detail()
{
//Intent detail=new Intent(con, Detail.class);
Intent grid=new Intent(this, Grid.class);
grid.putExtra("IDP", id);
this.startActivity(grid);
//Toast.makeText(this, "list : " , Toast.LENGTH_SHORT).show();
}
}
This is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Mulai dari :" />
<TextView
android:id="#+id/DateStar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="#+id/btn_PickDateStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tanggal Awal" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sampai dengan :" />
<TextView
android:id="#+id/DateEnd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="" />
<Button
android:id="#+id/btn_PickDateEnd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tanggal Akhir" />
<TextView
android:id="#+id/selection"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false" />
</LinearLayout>
Use the long value that Date gives you, and put it in the extras, along with everything else you are sending to the activity.