helpme for TimePickerDialog and date now - java

I am using a button which shows the hour and minute of the team with this method
private void obtenerFechaActual() {
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);
}
btnSelectTime.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Show the TimePickerDialog
obtenerFechaActual();
showDialog(TIME_DIALOG_ID);
}
});
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
}
return null;
}
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int min) {
hour = hourOfDay;
minute = min;
btnSelectTime.setText("Time selected :"+hour+"-"+minute);
}
};
It does well, but every time I press the button,it keeps the minutes and the date of the first click of the button. The idea is that every time you click the button, it displays the current time and date.

add this method:
#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;
}
}

Related

Spinner with only month and year in Android

One of the requirements in my app is a DatePickerDialog that shows only the month and year without the day.
This is my code :
//define format of YYYYMMDD
private final DateTimeFormatter dtf = DateTimeFormatter.BASIC_ISO_DATE;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_salary);
textView = (TextView) findViewById(R.id.spinner);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
SupportedDatePickerDialog dpd = new SupportedDatePickerDialog(this, R.style.SpinnerDatePickerDialogTheme, new SupportedDatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(#NonNull DatePicker datePicker, int year, int month, int day) {
String date = LocalDate.of(year, month+1, day).format(dtf);
textView.setText(date);
}
}, mYear, mMonth, mDay);
dpd.show();
}
});
}
I'm using a 3rd party library from Ibotta because I only want to show a Spinner and not a Calendar when choosing a date.
Everything else I tried was either deprecated or didn't work properly.
I was wondering if it is possible to reconfigure this piece of code so that the DatePickerDialog will only show the Month and Year without the Day ?
You can add this library
Click here
and then you can use MonthDayPicker
new DialogPlusBuilder().blurBackground()
.buildMonthDayPickerDialog(Calendar.getInstance(),
(pickedMonth, pickedDay) ->{ //TODO implement your business}
.show(getSupportFragmentManager(), "Month Day Picker");

How to access parameters from one method to another one

I'm stuck at here in this program,I want to access parameters of DatePickerFragment into TimePickerFragment.Please help.
public class datepicker extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datepicker);
Button btn = (Button) findViewById(R.id.datepick);
Button btn2 = (Button) findViewById(R.id.timepick);
// TextView textview = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogFragment dFragment = new DatePickerFragment();
// Show the date picker dialog fragment
dFragment.show(getFragmentManager(), "Date Picker");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DialogFragment dFragment = new TimePickerFragment();
// Show the time picker dialog fragment
dFragment.show(getFragmentManager(),"Time Picker");
}
});
}
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_DARK, this, year, month, day);
return dpd;
}
public void onDateSet(DatePicker view, int year, int month, int day) {
// Do something with the chosen date
Button textview = (Button) getActivity().findViewById(R.id.datepick);
textview.setText(day + ":" + (month + 1) + ":" + year);
}
}
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener{
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Get a Calendar instance
final Calendar calendar = Calendar.getInstance();
// Get the current hour and minute
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog tpd = new TimePickerDialog(getActivity(),
AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, this, hour, minute, false);
return tpd;
}
public void onTimeSet(TimePicker timePicker, int i, int i1) {
Button tv = (Button) getActivity().findViewById(R.id.timepick);
tv.setText(i + ":" + i1);
DatePickerFragment b = new DatePickerFragment();
b.onDateSet(view ,int year,int month,int day);
}
}}
I need year,month,day in TimePickerFragment method.Please solve it asap.Thanks in advance.

Cannot set Text for a button in onTimeSet

Here is my code, in my ListViewAdapter. I want to show a timePicker to set time for my Adapter. But a cannot solve my onTimeSet
p is my Adapter
assert btnLeft != null;
btnLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
#Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
p.setOnHour(hourOfDay);
p.setOnMinutes(minute);
btnLeft.setText(hourOfDay + " : " + minute);
}
}, p.getOnHour(), p.getOnMinutes(), false);
mTimePicker.setTitle("Select Time");
mTimePicker.show();
notifyDataSetChanged();
}
According to the docs, OnTimeSetListener calls onTimeSet() when
... the user is done setting a new time and the dialog has closed.
You need to use an OnTimeChangedListener with the onTimeChanged() method.

Android DatePicker showing Full Screen on Initialization

I have followed this tutorial to integrate a datepicker into my app, I have tailored the original to suit my needs and managed to get it working. Unfortunately the author doesn't go too much into the concepts of actually using the DatePicker dialog.
As a result, a full screen calendar is shown when the app is launched, whereas I am simply trying to pull the day, month and year when the button 'Pick Date' is clicked, what do I need to remove?
Code here:
public class MainActivity extends Activity implements OnClickListener,
OnCheckedChangeListener {
TabHost th;
Button changeDate;
TextView displayDate;
public static final int dDialogId = 1;
// date and time
private int mYear;
private int mMonth;
private int mDay;
#Override
protected void onCreate(Bundle savedInstanceState) {
// Set activity to full screen!!
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
th = (TabHost) findViewById(R.id.tabhost);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
// one tab
specs.setContent(R.id.tab1);
// what appears on actual tab
specs.setIndicator("Tools");
th.addTab(specs);
specs = th.newTabSpec("tag2");
// one tab
specs.setContent(R.id.tab2);
// what appears on actual tab
specs.setIndicator("Calorie Calculator");
th.addTab(specs);
specs = th.newTabSpec("tag3");
// one tab
specs.setContent(R.id.tab3);
// what appears on actual tab
specs.setIndicator("Your Stats!");
th.addTab(specs);
initializeVariables();
}
private void initializeVariables() {
mapARun = (Button) findViewById(R.id.btnMapARun);
weightConverter = (Button) findViewById(R.id.btnWeightConverter);
changeDate = (Button) findViewById(R.id.btnChangeDate);
displayDate = (TextView) findViewById(R.id.tvDisplayDate);
changeDate.setOnClickListener(this);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
updateDisplay();
}
#Override
#Deprecated
protected void onPrepareDialog(int id, Dialog dialog) {
// TODO Auto-generated method stub
super.onPrepareDialog(id, dialog);
((DatePickerDialog) dialog).updateDate(mYear, mMonth, mDay);
}
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 void updateDisplay() {
displayDate.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-").append(mDay).append("-")
.append(mYear));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnChangeDate:
DatePickerDialog DPD = new DatePickerDialog(this,
mDateSetListener, mYear, mMonth, mDay);
DPD.show();
break;
}
}
}
}
Apologies if you hit syntax errors when the codei s run, I cut out a lot of the other, unrelated functions,
Is there another way to just show the datepicker when the dialog is called?
Create yout main category with just the button.
Make the button call another activity with your code.
You'll get a screen with a button that calls another screen with a fullscreen datepicker.
Make the DatePicker Activity end itself when date is selected.
Make sure you are calling the activity with startActivityForResult and end it with finishWithResult.

problems with date picker in an android app

hi i my app i have placed two edit text boxes, when i touch on it the date picker dialog box gets appeared.
Now the problem is when i touch the first edit box the dialog opens and after setting it displays at the EditText1. Then when i touch the second edit box the dialog opens and after setting some other date, it is not displayed in EditText2, instead it is show in the EditText1 and the former date gets changed
I want the dates to be displayed in respective boxes.
The following is my code
{
et1 =(EditText)findViewById(R.id.widget29);
et1.setHint("DOB");
et2 =(EditText)findViewById(R.id.widget32);
et2.setHint("DOF");
et1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
et2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
// get the current date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
private void updateDisplay()
{
this.et1.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append("-"));
}
private void updateDisplay1()
{
this.et2.setText(
new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-")
.append(mDay).append("-")
.append(mYear).append("-"));
}
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 DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener()
{
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay1();
}
};
#Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
return null;
}
}
the following are some more problems i am facing here,
Whenever i touch the edit text box the keyboard also gets opened how to hide the keyboard
in the EditText box i want the order to be viewed as Year/Month/Date
Is there any way to change the date picker dialog box as the below figure
please help me in this
Check this code..
public class Main extends Activity {
EditText et1,et2;
static final int DATE_DIALOG_ID = 0;
static final int DATE_DIALOG_ID1 = 1;
private int mYear;
private int mMonth;
private int mDay;
private int mYear1;
private int mMonth1;
private int mDay1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1=(EditText)findViewById(R.id.EditText01);
et2=(EditText)findViewById(R.id.EditText02);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
et1.setText("month/year");
et2.setText("month/year");
et1.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID);
return false;
}
});
et2.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
showDialog(DATE_DIALOG_ID1);
return false;
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
case DATE_DIALOG_ID1:
return new DatePickerDialog(this, mDateSetListener1, mYear1, mMonth1,
mDay1);
}
return null;
}
// updates the date in the TextView
private void updateDisplay() {
et1.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth + 1).append("-").append(mYear));
}
private void updateDisplay1() {
et2.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mMonth1 + 1).append("-").append(mYear1));
}
// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDisplay();
}
};
private DatePickerDialog.OnDateSetListener mDateSetListener1 = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year1, int monthOfYear1,
int dayOfMonth1) {
// TODO Auto-generated method stub
mYear1 = year1;
mMonth1 = monthOfYear1;
mDay1 = dayOfMonth1;
updateDisplay1();
}
};
}
You're setting the same id for both show dialog methods...
et1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
et2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
showDialog(DATE_DIALOG_ID);
}
});
...and your switch/case block only has one 'case'....
switch (id)
{
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay);
}
As for the other issues, split them into separate questions.

Categories