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");
Related
I was trying to make an EditText where when I click on it a DatePickerDialog will show up so I can choose my birthday and then the date I choose will be written in the EditText. I found this code online but I am having problems with it as the line where it says:
new DatePickerDialog(TheClassName.this, datePickerListener, myCalendar..
The datePickerListener is colored red and I don't understand why? If someone can help me I would really appreciate it.
//some of the needed imports
import android.widget.DatePicker;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
//private field variables in the Activity class
private Calendar myCalendar = Calendar.getInstance();
private EditText etDate;
//onCreate method of the Activity class
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etDate = (EditText) findViewById(R.id.et_date);
etDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DatePickerDialog(TheClassName.this, datePickerListener, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
etDate.setText(sdf.format(myCalendar.getTime()));
}
};
}
You need to do some changes as below
Initialize the myCalendar to avoid NullPointerException
myCalendar = new GregorianCalendar();
Declare datePickerListener before using it to avoid Unresolved
variable
Change TheClassName.this to your activity class name to get the
context; I assumed below it as MainActivity.this
Make EditText & DatePickerDialog.OnDateSetListener as final in order to access them from the EditText OnClickListener callback
So with these changes in place, you can replace your code with below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText etDate = (EditText) findViewById(R.id.et_date);
myCalendar = new GregorianCalendar();
final DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String myFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
etDate.setText(sdf.format(myCalendar.getTime()));
}
};
etDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this, datePickerListener, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
My previous question get the value from a method in another class is understandably closed because its duplicate, but I still can't find a way to solve the problem. I will be grateful if someone kindly help. I have progressed a little, but thats not enough.
First, my current code:
MainActivity.java
public class MainActivity extends AppCompatActivity
implements CalFragment.OnDateSelected{
private TextView mTextMessage;
String date = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date());
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.menu_home);
return true;
case R.id.navigation_dashboard:
//mTextMessage.setText(date);
showDateDialog();
mTextMessage.setText(date);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.menu_location);
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
public void showDateDialog() {
DialogFragment CalFragment = new CalFragment();
CalFragment.show(getSupportFragmentManager(), "timePicker");
//date = "Hello";
}
}
and
CalFragment.java
public class CalFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
OnDateSelected mcallback;
public interface OnDateSelected {
public void NewStrDate(String strDate);
}
#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
SimpleDateFormat dateFormatter = new SimpleDateFormat("MM-dd-yyyy");
Date d = new Date(year, month, day);
String strDate = dateFormatter.format(d);
}
}
I want to supply the date from CalFragment to ShowDate dialogue, but its not working. At the present state, it shows implements declaration in MainActivity is faulty. I am reading some java tutorial, but its just my 2nd day. So kindly help a bit.
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.
I am trying to set an editText with a DatePickerDialog inside a .setOnClickListener. where, when the user clicks on the editText the DatePickerDialog will appear and then after the user selects the desirable date it will set it on the editText... something like this : Android:DatePickerDialog on EditText Click Event
and here is my code mycode
Any ideas how to do that?
I tried the above example and many more but nothing seems to work inside the .SetOnClickListener
Use this code:
public class MainActivity extends AppCompatActivity {
EditText text;
private int year,currentYear;
private int month,currentMonth;
private int day,currentDay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (EditText)findViewById(R.id.edit);
// Get current date by calender
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker datePicker, int mYear, int mMonth, int mDay) {
currentDay = mDay;
currentMonth = mMonth+1;
currentYear = mYear;
text.setText(mDay+"-"+(mMonth+1)+"-"+mYear);
}
},year,month,day).show();
}
});
}
}
I have store the user selected year, month, day in currentYear,currentMonth,currentDay respectively.
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;
}
}