LayoutInflater with DatePicker Dialog - java

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.

Related

Showing data using DatePickerDialog on EditText

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();
}
});
}

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");

Trying to save notes for each day of a calendar year android studio

I don't get any errors but this doesn't work every time I try to change date in the calendar my app stops working and even when I press the save button it also keeps stop working before I add the code with the array it was working properly
public class Calendar extends AppCompatActivity {
private CalendarView calendarView;
private TextView currentDate;
private Button btnsavenote;
private EditText newNote, showNote;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
calendarView = (CalendarView) findViewById(R.id.calen_v);
currentDate = (TextView) findViewById(R.id.currentDate);
btnsavenote = (Button) findViewById(R.id.save_note);
newNote = (EditText) findViewById(R.id.newNote);
showNote = (EditText) findViewById(R.id.shownote);
final String [][] array = new String[31][12];
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(#NonNull CalendarView view, int year, int month, int dayOfMonth) {
String tDate = (dayOfMonth) + "/" + month + "/" + year;
currentDate.setText(tDate);
}
});
btnsavenote.setOnClickListener(new View.OnClickListener() {
String MytextMessage = newNote.getText().toString();
#Override
public void onClick(View v) {
int a;
int b;
a = Integer.parseInt(array [30][2]);
b = Integer.parseInt(array [31][2]);
array[a][b] = MytextMessage;
}
});
}
}

"cannot find symbol method setOnDismissListener(<anonymous OnDismissListener>)"

So, I try to set an OnDismissListener on a dialog thing.
Datepicker dialog = new Datepicker(v);
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface Dialog) {
mGoogleMap.clear();
setMap(mGoogleMap);
}
});
But it says
"Cannot resolve method 'setOnDismissListener(anonymous android.content.DialogInterface.OnDismissListener)'"
The Datepicker class extends DialogFragment, so it should have the setOnDismissListener method?
I've imported android.content.DialogInterface'. Is it confused on thenew DialogInterface.onDismissListener()` for some reason?
Edit: Someone asked for some of the Datepicker code. So here's the constructor and stuff. Let me know if you need anything else.
public class Datepicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText txtDate;
String strdate;
public Datepicker(View v){
txtDate = (EditText)v;
}
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);
SimpleDateFormat simpleformat = new SimpleDateFormat("MM/dd/yyyy");
strdate = simpleformat.format(c.getTime());
return new DatePickerDialog(getActivity(), this, year, month, day);
}
}
You have to resolve this issue with a help of interface
public class Main3Activity extends AppCompatActivity implements DatePickerListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.........
//Initialise or create your dialog like this
DatePicker dialog = new DatePicker(v,this);
}
#Override
public void onDatePickerDismissed() {
//Here You receive the dialog dissmiss listner
mGoogleMap.clear();
setMap(mGoogleMap);
}
}
Create a interface class like
public interface DatePickerListener {
public void onDatePickerDismissed();
}
Change you dialog picker like this
#SuppressLint("ValidFragment")
public class DatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {
EditText txtDate;
String strdate;
DatePickerListener datePickerListener;
#SuppressLint("ValidFragment")
public DatePicker(View v, DatePickerListener _datePickerListener){
txtDate = (EditText)v;
this.datePickerListener = _datePickerListener;
}
public DatePicker() {
}
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);
SimpleDateFormat simpleformat = new SimpleDateFormat("MM/dd/yyyy");
strdate = simpleformat.format(c.getTime());
return new DatePickerDialog(getActivity(), this, year, month, day);
}
#Override
public void onDateSet(android.widget.DatePicker datePicker, int i, int i1, int i2) {
}
#Override
public void onDismiss(DialogInterface dialog) {
datePickerListener.onDatePickerDismissed();
super.onDismiss(dialog);
}
}
Hope it will help you :)

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.

Categories