Fragmentss !! how can I use this as a fragment - android

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);
}
};
}
Can someone help me how to use date picker code with fragments please
I am trying to get the user to select a date , work out the difference between the dates from the current date and the date they selected then divide the date difference with the number of pounds (weight) they want to loose so a message appears as in an empty string displaying the target. for example todays date is 14/12/2012 and the user selects 24/12/2012 the date difference is 10 days and they wish to loose 10pounds(weight) in 10 days
how can i programme for it to work out the difference / it by the goal and display a result showing the aim which will be 1pound per day
Any help will be great i am really lost thankss !

you have extend the class Fragment first.
then you override onCreateView() to inflate your contentview.
the you use onActivityCreated you bind your views.

Related

Android: DatePicker value saved in a variable doesn't go in onCreate

I have a rather simple issue but this is bugging me out!
I am trying to build an app in which I have 2 DatePickers, periodFrom and periodTo.
Basically what I'm trying to do is let the user pick dates on both cases and then calculate the difference between dates in days. I already know how to get the difference, how to get the date from the DatePicker. The problem is I don't know where I should get the value from the DatePicker, as my onCreate intializes the listener for the DatePickers but it doesn't store the set dates in the global variables, only the current date which it gets from Calendar.getInstance();
Here is my code for the MainActivity, maybe you can enlighten me on this issue I'm having. Thank you!
MainActivity.class
package com.endtech.utilitycalculator;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private EditText periodFrom, periodTo;
private Calendar mCalendarFrom = Calendar.getInstance();
private Calendar mCalendarTo = Calendar.getInstance();
private long from, to;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
periodFrom = (EditText) findViewById(R.id.periodFrom);
periodTo = (EditText) findViewById(R.id.periodTo);
initListeners();
from = mCalendarFrom.getTimeInMillis(); //This is current time, not the set time
to = mCalendarTo.getTimeInMillis(); //This is current time, not the set time
}
private void initListeners() {
setDateFrom();
setDateTo();
}
private void setDateFrom() {
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendarFrom.set(Calendar.YEAR, year);
mCalendarFrom.set(Calendar.MONTH, monthOfYear);
mCalendarFrom.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelFrom();
}
};
periodFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, date, mCalendarFrom.get(Calendar.YEAR),
mCalendarFrom.get(Calendar.MONTH), mCalendarFrom.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabelFrom() {
String mFormat = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(mFormat, Locale.GERMANY);
periodFrom.setText(sdf.format(mCalendarFrom.getTime()));
}
private void setDateTo() {
final DatePickerDialog.OnDateSetListener date2 = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendarTo.set(Calendar.YEAR, year);
mCalendarTo.set(Calendar.MONTH, monthOfYear);
mCalendarTo.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelTo();
}
};
periodTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, date2, mCalendarTo.get(Calendar.YEAR),
mCalendarTo.get(Calendar.MONTH), mCalendarTo.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabelTo() {
String mFormat = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(mFormat, Locale.GERMANY);
periodTo.setText(sdf.format(mCalendarTo.getTime()));
}
}
The problem is you are getting the dates before selecting them.
Simply Create a button (when you click on the button then the process of converting difference into days get done). then on button click get the values of dates and then convert it into days.
Let me know if you find some problem by commenting below.

DatePickerDialog showing abnormal behaviour in android 5.1 when timezone is changed

I have to show calender using DatePickerDialog.
The below code is used:
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TextView;
import android.widget.Toast;
public class DatePickerExample extends Activity {
private TextView Output;
private Button changeDate;
private Boolean mEnableNativeCalGridView = null;
String timeZone;
public static TimeZone tz;
private int year;
private int month;
DatePickerDialog d;
public Calendar c;
SimpleDateFormat sdf;
private int day;
public static IntentFilter s_intentFilter;
static final int DATE_PICKER_ID = 1111;
static{
s_intentFilter = new IntentFilter();
s_intentFilter.addAction(Intent.ACTION_TIME_TICK);
s_intentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
s_intentFilter.addAction(Intent.ACTION_TIME_CHANGED);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(m_timeChangedReceiver, s_intentFilter);
Output = (TextView) findViewById(R.id.Output);
changeDate = (Button) findViewById(R.id.changeDate);
// sdf = new SimpleDateFormat("EEE, MMM d, ''yy");//Wed, Jul 4, '01
// Show current date
Output.setText(new StringBuilder()
// Month is 0 based, just add 1
.append(month + 1).append("-").append(day).append("-")
.append(year).append(" "));
// Button listener to show date picker dialog
changeDate.setOnClickListener(new OnClickListener() {
#SuppressWarnings("deprecation")
#Override
public void onClick(View v) {
// On button click show datepicker dialog
showDialog(DATE_PICKER_ID);
}
});
}
#Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DATE_PICKER_ID:
((DatePickerDialog) dialog).updateDate(
c.get(Calendar.YEAR),
c.get(Calendar.MONTH),
c.get(Calendar.DAY_OF_MONTH));
}
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_PICKER_ID:
// open datepicker dialog.
// set date picker for current date
// add pickerListener listner to date picker
sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
tz = TimeZone.getDefault();
System.out.println("TimeZone "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID());
timeZone = tz.getDisplayName(false, TimeZone.SHORT);
c= Calendar.getInstance();
c.setTimeZone(tz);
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
d = new DatePickerDialog(DatePickerExample.this, pickerListener, year, month,day);
setMinMaxdate();
return d;
}
return null;
}
////////////////////Fix for issue on OS 5.1 //////////////////////
public long getDateFormatted(String date){
String givenDateString = date;
long timeInMilliseconds = 0;
try {
Date mDate = sdf.parse(givenDateString);
timeInMilliseconds = mDate.getTime();
System.out.println("Date in milli :: " + timeInMilliseconds);
} catch (ParseException e) {
e.printStackTrace();
}
return timeInMilliseconds;
}
//////////////////////////////////////////
private DatePicker.OnDateChangedListener newchange= new OnDateChangedListener(){
#Override
public void onDateChanged(DatePicker view,
int year, int monthOfYear,int dayOfMonth) {
Toast.makeText(getApplicationContext(),
"onDateChanged", Toast.LENGTH_SHORT).show();
}};
DatePicker datePicker;
public void setMinMaxdate() {
//Time zone calculation
//TimeZone.setDefault(tz);
///////////////////////
long calEvtEndDate= getDateFormatted("Sun May 31 23:59:59 "+timeZone+" 2015");//System.currentTimeMillis();
long calEvtStartDate= getDateFormatted("Wed May 13 00:00:00 "+timeZone+" 2015");//System.currentTimeMillis()/2;
// long calEvtEndDate = getDateFormatted("Sun, may 31, '15");
//long calEvtStartDate = getDateFormatted("Wed, may 13, '15");
if(d != null){
datePicker = d.getDatePicker();
if(mEnableNativeCalGridView != null){
datePicker.setCalendarViewShown(mEnableNativeCalGridView.booleanValue());
}
// If Start Date is Greater than End Date then we are showing from valid StartDate
// value and we are not setting the maxdate.
if (calEvtStartDate > calEvtEndDate) {
datePicker.setMinDate(calEvtStartDate);
} else {
if (calEvtStartDate > 0) { // If Only ValidStart date is provided, then setting the minDate.
datePicker.setMinDate(calEvtStartDate);
}
if (calEvtEndDate > 0) { // If Only ValidEnd date is provided, then setting the maxDate.
datePicker.setMaxDate(calEvtEndDate);
}
}
}
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println("-------resumed");
}
public DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {
// when dialog box is closed, below method will be called.
#Override
public void onDateSet(DatePicker view, int selectedYear,
int selectedMonth, int selectedDay) {
//view.updateDate(year, month, day);
System.out.println("---------------datesetchange");
year = selectedYear;
month = selectedMonth;
day = selectedDay;
// Show selected date
Output.setText(new StringBuilder().append(month + 1)
.append("-").append(day).append("-").append(year)
.append(" "));
}
};
public void onDestroy() {
super.onDestroy();
unregisterReceiver(m_timeChangedReceiver);
}
private final BroadcastReceiver m_timeChangedReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(Intent.ACTION_TIME_CHANGED) ||
action.equals(Intent.ACTION_TIMEZONE_CHANGED))
{
System.out.println("timezone changed---"+action.toString());
tz = TimeZone.getDefault();
System.out.println("TimeZone "+tz.getDisplayName(false, TimeZone.SHORT)+" Timezon id :: " +tz.getID());
timeZone = tz.getDisplayName(false, TimeZone.SHORT);
// Intent intent1 = getIntent();
// finish();
// startActivity(intent1);
showDialog(DATE_PICKER_ID);
}
}
};
}
When the app is running, I go to the settings and change the Timezone to such a value so that the current date will change. Now when we select any date in the dialog, it behaves abnormally and goes to other date. I have used setMinDate() and setMaxDate() methods. This happens only in Android OS 5.1.
Any idea or help? Thanks in advance.

Changing the layout of DatePicker

Is there any way to reformat the datePicker so that instead of getting "mm/dd/yyyy" you can get "dd/mm/yyyy" or even "dd/mm/yy"
This is my current code.
import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.app.DatePickerDialog.OnDateSetListener;
import java.util.Calendar;
/**
* Created by MOS182 on 7/21/13.
*/
public class AddReminder extends Activity {
TextView Title, Amount, PaymentDate, ReminderDate, ReminderTime;
EditText eTitle, eAmount, ePaymentDate, eReminderDate, eReminderTime;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.reminders_dialog);
initializeVariables();
ePaymentDate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//To show current date in the datepicker
Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker;
mDatePicker = new DatePickerDialog(AddReminder.this, new OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
/* Your code to get date and time */
selectedmonth = selectedmonth + 1;
ePaymentDate.setText("" + selectedday + "/" + selectedmonth + "/" + selectedyear);
}
}, mYear, mMonth, mDay);
mDatePicker.setTitle("Select date");
mDatePicker.show();
}
});
}
private void initializeVariables()
{
Title = (TextView) findViewById(R.id.tvTitle);
Amount = (TextView) findViewById(R.id.tvAmount);
PaymentDate = (TextView) findViewById(R.id.tvPaymentDate);
ReminderDate = (TextView) findViewById(R.id.tvReminderDate);
ReminderTime = (TextView) findViewById(R.id.tvReminderTime);
eTitle = (EditText) findViewById(R.id.etTitle);
eAmount = (EditText) findViewById(R.id.etAmount);
ePaymentDate = (EditText) findViewById(R.id.etPaymentDate);
eReminderDate = (EditText) findViewById(R.id.etReminderDate);
eReminderTime = (EditText) findViewById(R.id.etReminderTime);
}
}
This is what is currently displayed when I run my code and select the ePaymentDate field.
The picker take the date format chosen by the user, which means you don't really have to format it, as probably the user enjoys the most to see the format he's used to.
I just tested a code and on my phone, where I have the date in (dd/mm/yyyy) format, so the picker shows the same format; in emulator, I've put the date in mm/dd/yyyy format, so the picker displays the same.
So there is no method to set the display format.
But if you still really want to display a specific format, then refer to this link, there is an elaborate way to show the date in the desired format, but with changes in the original code.

Datepicker created with DialopFragment showing Calender also

I am new to Android programming and I am using a basic Datepicker created using a DialogFragment. My DatePicker shows up fine on click etc., but the problem is that it is displaying the Calendar view as well. I do not want this. I have been searching for a solution for a day now, and I am unwilling to use custom Datepickers right now, as I want to get familiar with the basics first.
I have also read suggestions such as
yourDatepicker.setCalendarViewShown(false);
or set it to false in the XML. But my DatePicker comes from a DialogFragment, so how do I access this datepicker? How do I set its final view? I do not wish to make changes in the source code as I am still learning the ropes.
My datepicker code:
public class DueDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day){
// Code to set Due Date TextField to the selected date.
TextView reminderText = (TextView)getActivity().findViewById(R.id.addTaskFormDueDateHolder);
String dueDateStr = year + "-" + month + "-" + day;
reminderText.setText(dueDateStr);
}
}
I have used Datepicker Dialog Fragment, to show the Date picker in dialog.
Here is my Complete Code
MainActivity.java
package com.example.testmydrag;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.DatePicker;
import android.support.v4.app.FragmentActivity;
import android.app.Dialog;
import android.app.DatePickerDialog;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;
public class MainActivity extends FragmentActivity {
EditText mEdit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void selectDate(View view) {
DialogFragment newFragment = new SelectDateFragment();
newFragment.show(getSupportFragmentManager(), "DatePicker");
}
public void setTheDate(int year, int month, int day) {
mEdit = (EditText)findViewById(R.id.Text);
mEdit.setText(month+"/"+day+"/"+year);
}
/*
* Date Picker Dialog
*/
public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int yy = c.get(Calendar.YEAR);
int mm = c.get(Calendar.MONTH);
int dd = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(getActivity(),this,c.YEAR, c.MONTH, c.DATE);
/*Calendar View if you want to Remove Set it to False*/
dialog.getDatePicker().setCalendarViewShown(true);
/*Spinner View if you want to Show Set it to True*/
dialog.getDatePicker().setSpinnersShown(false);
dialog.setTitle("Pick a date");
return dialog;
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
setTheDate(yy, mm+1, dd);
}
}
}
main.xml
<?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"
>
<EditText android:text="#+string/date_text"
android:id="#+id/Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/pick_date"
android:onClick="selectDate" />
</LinearLayout>
I found a temporary solution, though I am not entirely happy with it. You need minimum SDK version 11 for this.
Basically I added a call to getDatePicker() in my onAcreateDialog function, mentioned in the DueDatePickerFragment, and then changed its settings. I don't understand why I need to do this, and why it is appearing so incorrectly in my emulator. No one else seems to be having this problem. Any explanations would be highly appreciated. Here is the code:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState){
Log.i("DUEDATEPICKERDIALOG", "Inside onCreateDIALOG");
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialogObj = new DatePickerDialog(getActivity(), this, year, month, day);
DatePicker dueDatePicker = datePickerDialogObj.getDatePicker();
float scaleVal = (float) 0.6;
dueDatePicker.setScaleX(scaleVal);
dueDatePicker.setScaleY(scaleVal);
dueDatePicker.setCalendarViewShown(false);
return datePickerDialogObj;
}
Hope this helps someone.

DatePicker Example in android

Please suggest me some tutorial which gives the example for DatePicker and how to use its methods like OnDateChangedListener, onDateChanged etc. Actually I am going through some sites, but i did not get the clear idea of it.
Thank you
Android references on DatePicker is quite good. Have a look at it here.
private DatePicker datePicker;
//monthofYear is between 0-11
datePicker.init(2010, 11, 1, new OnDateChangedListener() {
#Override
public void onDateChanged(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
// Notify the user.
}
});
See.Example(); here
Check this Data Picker example: Example of DATE PICKER .
Step 1 : create a java file:
package com.example.babs;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.app.FragmentManager;
public class EditUserInfo extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.user_edit_view);
}
public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
// pgrm mark ---- ---- ----- ---- ---- ----- ---- ---- ----- ---- ---- -----
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
}
}
public void showDatePickerDialog(View v) {
FragmentManager fragmentManager = getFragmentManager();
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(fragmentManager, "datePicker");
}
}// end main class EditUserInfo
step 2: your xml file must contain :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:fillViewport="true" >
</ScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pick_date"
android:onClick="showDatePickerDialog" />
You can try this code:
public
static class DatePickerFragment extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
DateEdit.setText(day + "/" + (month + 1) + "/" + year);
}
}
Taken from Example of DatePickerFragment and TimePickerFragment.

Categories

Resources