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
Related
Iam using Material Design Datepicker i can be able to set from tomorrows date but todays date is also showing in calendar i set for 29/11/2016 but it also showing 28/11/2016
How to disable that so that user cant select it
Iam using Material Design DatePicker
https://github.com/wdullaer/MaterialDateTimePicker
GregorianCalendar g1=new GregorianCalendar();
g1.add(Calendar.DATE, 1);
GregorianCalendar gc = new GregorianCalendar();
gc.add(Calendar.DAY_OF_MONTH, 30);
dateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DatePickerDialog datePickerDialog ;
datePickerDialog = DatePickerDialog.newInstance(LoginSuccess.this, g1.get(Calendar.YEAR),
g1.get(Calendar.MONTH),
g1.get(Calendar.DAY_OF_MONTH));
datePickerDialog.setTitle("Select Date");
//datePickerDialog.setMinDate(g);
GregorianCalendar c2=new GregorianCalendar();
c2.setTimeInMillis(g1.getTimeInMillis()+(27*7*24*60*60*1000));
//c2.add(Calendar.DAY_OF_MONTH, 30);
List<Calendar> dayslist= new LinkedList<Calendar>();
Calendar[] daysArray;
Calendar cAux = Calendar.getInstance();
while ( cAux.getTimeInMillis() <= gc.getTimeInMillis())
{
if (cAux.get(Calendar.DAY_OF_WEEK)!=1)
{
Calendar c = Calendar.getInstance();
c.setTimeInMillis(cAux.getTimeInMillis());
dayslist.add(c);
}
cAux.setTimeInMillis(cAux.getTimeInMillis()+(24*60*60*1000));
}
daysArray = new Calendar[dayslist.size()];
for (int i = 0; i<daysArray.length;i++)
{
daysArray[i]=dayslist.get(i);
}
//g1.setTimeInMillis(g.getTimeInMillis()+(7*24*60*60*1000));
datePickerDialog.setMinDate(g1);
datePickerDialog.setMaxDate(c2);
datePickerDialog.show(getFragmentManager(), "DatePickerDialog");
datePickerDialog.setSelectableDays(daysArray);
}
Try this out!
dateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
now.add(Calendar.DAY_OF_YEAR, 1);
DatePickerDialog dpd = DatePickerDialog.newInstance(
MainActivity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setMinDate(now);
dpd.setThemeDark(modeDarkDate.isChecked());
dpd.vibrate(vibrateDate.isChecked());
dpd.dismissOnPause(dismissDate.isChecked());
dpd.showYearPickerFirst(showYearFirst.isChecked());
if (modeCustomAccentDate.isChecked()) {
dpd.setAccentColor(Color.parseColor("#9C27B0"));
}
if(titleDate.isChecked()) {
dpd.setTitle("DatePicker Title");
}
dpd.show(getFragmentManager(), "Datepickerdialog");
}
});
}
Hey i have done sample regarding the issue and able to get the date picker as you expect once check it and let me know
Java:
package yourpackage;
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.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private DatePicker datePicker;
private Calendar calendar;
private TextView dateView;
private int year, month, day;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dateView = (TextView) findViewById(R.id.textView3);
calendar = Calendar.getInstance();
year = calendar.get(Calendar.YEAR);
month = calendar.get(Calendar.MONTH);
day = calendar.get(Calendar.DAY_OF_MONTH);
showDate(year, month + 1, day);
}
#SuppressWarnings("deprecation")
public void setDate(View view) {
showDialog(1);
Toast.makeText(getApplicationContext(), "ca", Toast.LENGTH_SHORT)
.show();
}
#Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if (id == 1) {
return new DatePickerDialog(this, myDateListener, year, month,
day + 1);
}
return null;
}
private DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
// arg1 = year
// arg2 = month
// arg3 = day
showDate(arg1, arg2 + 1, arg3);
}
};
private void showDate(int year, int month, int day) {
dateView.setText(new StringBuilder().append(day).append("/")
.append(month).append("/").append(year));
}
}
Layout:
<?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"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:onClick="setDate"
android:text="#string/date_button_set" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="#string/date_label_set"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_marginTop="66dp"
android:layout_toLeftOf="#+id/button1"
android:text="#string/date_view_set"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button1"
android:layout_below="#+id/textView2"
android:layout_marginTop="72dp"
android:text="#string/date_selected"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
Values
<resources>
<string name="app_name">Androidr</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="text_label_1">Rate</string>
<string name="button_label">Submit</string>
<string name="text_label_2">Result: </string>
<string name="action_settings">Settings</string>
<string name="date_label_set">Press the button to set the date</string>
<string name="date_button_set">Set Date</string>
<string name="date_view_set">The Date is: </string>
<string name="date_selected"></string>
</resources>
g1.add(Calendar.DATE, +1);
check it like this
You should do this,
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, day + 1);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.YEAR, year);
datePickerDialog.getDatePicker().setMaxDate(maxDate.getTimeInMillis());
this will restrict you to select current date and let you select the
date starts from tomorrow.
From what I recall from the documentation for this library, I believe that if you set the selectable days for MaterialDateTimePicker then it overrides anything you have done set for min and max date. So if you pass an array of dates it will need to include your min and max dates, plus everything you want to see in between.
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.
I have Set Two Date Pickers(Namely Check In and Check Out). For the first time when I run the app everything is fine,the date which I clicked is displayed in the TextView.But without running the app second time When I try to click the Check In button the date is not changing in the TextView. But the date is changing in the LogCat. At the same time the Check Out button is working fine for the first time and also with second time(without running the app again). Kindly solve my issue.
Here is my code :
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Calendar;
public class MainActivity extends ActionBarActivity {
Button checkIn, checkOut, checkavailability;
TextView displayCheckIn, displayCheckOut;
int year, month, day;
static final int DATE_DIALOG_ID = 1;
static final int DATE_DIALOG_ID2 = 2;
int cur = 0;
String chidate, chodate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkIn = (Button) findViewById(R.id.checkInBtn);
checkOut = (Button) findViewById(R.id.checkOutBtn);
displayCheckIn = (TextView) findViewById(R.id.displayCheckIntv);
displayCheckOut = (TextView) findViewById(R.id.displayCheckOuttv);
checkavailability = (Button) findViewById(R.id.checkAvailabilityBtn);
setCurrentDateOnView();//declaring a method to display the current date in the opening
addListenerOnButton();
}
//settin the current date in the opening page
public void setCurrentDateOnView() {
Calendar today = Calendar.getInstance();
year = today.get(Calendar.YEAR);
month = today.get(Calendar.MONTH);
day = today.get(Calendar.DAY_OF_MONTH);
//displaying current date in the check in textView
displayCheckIn.setText(year + "-" + (month + 1) + "-" + day);
//displaying the current date in check out textView by getting the text from the check in view
displayCheckOut.setText(year + "-" + (month + 1) + "-" + day);
}
public void addListenerOnButton() {
checkIn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
checkOut.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(DATE_DIALOG_ID2);
}
});
checkavailability.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//checkAvailability();
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
System.out.println("onCreate Dialog :" + id);
cur = DATE_DIALOG_ID;
//Displaying the Date Picker Dialog on the screen with today's date
return new DatePickerDialog(this, datePickerListener, year, month, day);
case DATE_DIALOG_ID2:
System.out.println("onCreateDialog :" + id);
cur = DATE_DIALOG_ID2;
return new DatePickerDialog(this, datePickerListener, year, month, day);
}
return null;
}
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int syear, int monthOfYear, int dayOfMonth) {
year = syear;
month = monthOfYear;
day = dayOfMonth;
if (cur == DATE_DIALOG_ID) {
displayCheckIn.setText(year + "-" + (month + 1) + "-" + day);
chidate = displayCheckIn.getText().toString();
System.out.println(chidate);
//Toast.makeText(getApplicationContext(),chidate,Toast.LENGTH_LONG).show();
} else {
displayCheckOut.setText(year + "-" + (month + 1) + "-" + day);
chodate = displayCheckOut.getText().toString();
System.out.println(chodate);
//Toast.makeText(getApplicationContext(),chodate,Toast.LENGTH_LONG).show();
}
}
};
//This is the code which I want to check the Checkin date every time.
/*public void checkAvailability() {
String[] datesin = {"2014-12-16", "2014-12-17", "2014-12-18"};
for (int j = 0; j < datesin.length; j++) {
if (datesin[j].equals(chidate)) {
Toast.makeText(getApplicationContext(), "Room Not Available", Toast.LENGTH_LONG).show();
break;
} else
Toast.makeText(getApplicationContext(), "Room Available", Toast.LENGTH_LONG).show();
}
}*/
}
And My XML is :
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/displayCheckIntv"
android:text="Hiiiiiiiiiiiiiiiiiiiii"
android:layout_toEndOf="#+id/datePicker"
android:layout_alignTop="#+id/datePicker"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckIn"
android:id="#+id/checkInBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Out"
android:id="#+id/checkOutBtn"
android:layout_marginTop="48dp"
android:layout_below="#+id/checkInBtn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Out"
android:id="#+id/displayCheckOuttv"
android:layout_toEndOf="#+id/checkOutBtn"
android:layout_alignBottom="#+id/checkOutBtn"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Availability"
android:id="#+id/checkAvailabilityBtn"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/displayCheckOuttv"
android:layout_alignStart="#+id/displayCheckOuttv" />
</RelativeLayout>
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);
}
};
}